【转自】 http://hi.baidu.com/afantihome/blog/item/efbc82d3386dd4dda9ec9ace.html
C# winform 中如何设定让DATAGRID不绑定数据,而只做录入用?
我想让它像EXCEL表一样录入用,
private void MakeDataSet()
{
// Create a DataSet.
myDataSet = new DataSet("myDataSet");
DataTable tCust = new DataTable("Table1");
// Create two columns, and add them to the first table.
DataColumn col1 = new DataColumn("Col1");
DataColumn col2 = new DataColumn("Col2");
tCust.Columns.Add(col1);
tCust.Columns.Add(col2);
// Add the tables to the DataSet.
myDataSet.Tables.Add(tCust);
// Create three customers in the Customers Table.
tCust.Rows.Add(tCust.NewRow());
tCust.Rows[0]["Col1"]="Cell1,1";
tCust.Rows[0]["Col2"]="cell1,2";
}
private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Table1";
// Set other properties.
ts1.AlternatingBackColor = Color.LightGray;
//
// Add cols with DataGridComboBoxColumn column style
DataGridTextBoxColumn Col1 = new DataGridTextBoxColumn();
TextCol.MappingName = "FilterCondition";
TextCol.HeaderText = "列1";
TextCol.Width = 200;
ts1.GridColumnStyles.Add(Col1);
DataGridTextBoxColumn Col2 = new DataGridTextBoxColumn();
TextCol.MappingName = "FilterCondition";
TextCol.HeaderText = "列2";
TextCol.Width = 200;
ts1.GridColumnStyles.Add(Col2);
//add the custom table style to the datagrid table styles
dataGrid1.TableStyles.Add(ts1);
}