看MSDN例子是:
DataTable workTable = new DataTable("Customers");DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));这个是没有数据之前添加,要是有了数据,而且不知道具体是哪一列。怎么添加。其实就是想在DataTable前加一个序列的列,好像行号之类的东西。如何做。
DataTable workTable = new DataTable("Customers");DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));这个是没有数据之前添加,要是有了数据,而且不知道具体是哪一列。怎么添加。其实就是想在DataTable前加一个序列的列,好像行号之类的东西。如何做。
昏,我就是不知道怎么加,难道for循环一行行的加?
DataGrid中到可以指定
DataGrid1.Columns.AddAt("添加的索引","你的列");
怎么办?
DataColumn myDataColumn= customerTable.Columns.Add("ID", System.Type.GetType("System.Decimal"));
int i=0;
foreach( DataRow row in customerTable.Rows )
{
row["id"]= i++;
}
但是我想把它插到第一列,非要用for做吗?有没有其他办法?
dr = customerTable.NewRow();
dr[0] = 10;
dr[1] = "Item 10" ;
dr[2] = DateTime.Now;
dr[3] = true;
dr[4] = 1.23 * 11;
customerTable.Rows.InsertAt(dr,0);
customerTable.AcceptChanges();
然后再增加ID列
Of course, Add Column first, since Columns will determine the structure of the DataTable
And then, add new row into the DataTable in terms of its column structure.
However, I don't think the order of the columns in DataTable is very important, it is just a collection.You can adjust the display order when you bind DataTable to DataGrid or other DataBind controls