为什么我新建这个项目的时候提示没有安装.NET FRAMEWORK PC 2.0 可是从添加删除里看 已经安装了
最好的办法就是重装,装的时候要全选。
曾经的曾经已不在
试试这个
[CODE]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PocketTest
{
public partial class Form1 : Form
{
private DataTable namesTable ;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitTable();
}
private void InitTable()
{
this.namesTable = new DataTable("Names");
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "File";
idColumn.AutoIncrement = true;
namesTable.Columns.Add(idColumn);
DataColumn fNameColumn = new DataColumn();
fNameColumn.DataType = System.Type.GetType("System.String");
fNameColumn.ColumnName = "Name";
fNameColumn.DefaultValue = "name";
namesTable.Columns.Add(fNameColumn);
DataColumn numColumn = new DataColumn();
numColumn.DataType = System.Type.GetType("System.Int32");
numColumn.ColumnName = "Data";
numColumn.AutoIncrement = true;
namesTable.Columns.Add(numColumn);
}
private void button1_Click(object sender, EventArgs e)
{
namesTable.Rows.Add(namesTable.NewRow());
this.dataGrid1.DataSource = namesTable;
}
private void button2_Click(object sender, EventArgs e)
{
this.namesTable.Dispose();
InitTable();
this.dataGrid1.DataSource = namesTable;
}
}
}[/CODE]
用到的控件对应表
[CODE] private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;[/CODE]
试试下面这段代码可以达到你想要的效果
另外提醒手工在Datagrid的TableStyle属性里设置也可以达到同样效果
但是不能灵活控制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace PocketTest
{
public partial class Form1 : Form
{
private DataTable namesTable ;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitTable();
}
private void InitTable()
{
this.namesTable = new DataTable("Names");
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "File";
idColumn.AutoIncrement = true;
namesTable.Columns.Add(idColumn);
DataColumn fNameColumn = new DataColumn();
fNameColumn.DataType = System.Type.GetType("System.String");
fNameColumn.ColumnName = "Name";
fNameColumn.DefaultValue = "name";
namesTable.Columns.Add(fNameColumn);
DataColumn numColumn = new DataColumn();
numColumn.DataType = System.Type.GetType("System.Int32");
numColumn.ColumnName = "Data";
numColumn.AutoIncrement = true;
namesTable.Columns.Add(numColumn);
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = this.namesTable.TableName;
int columnNum = 3;
for (int i = 0; i < columnNum; i++)
{
DataGridColumnStyle dgcs = new DataGridTextBoxColumn();
dgcs.Width = (this.dataGrid1.Width - 20 - columnNum ) / columnNum;
dgcs.MappingName = this.namesTable.Columns[i].ColumnName;
dgcs.HeaderText = this.namesTable.Columns[i].ColumnName;
dgts.GridColumnStyles.Add(dgcs);
}
this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(dgts);
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox1.Text))
{
MessageBox.Show("Please Input One Number");
return;
}
for (int i = 0; i < int.Parse (this.textBox1.Text) ; i++)
{
namesTable.Rows.Add(namesTable.NewRow());
}
this.dataGrid1.DataSource = namesTable;
}
private void button2_Click(object sender, EventArgs e)
{
this.namesTable.Dispose();
InitTable();
this.dataGrid1.DataSource = namesTable;
}
}
}