有人知道在DataTable中在第0个位置插入列的最佳方法吗?
DataColumn Col = datatable.Columns.Add("Column Name", System.Type.GetType("System.Boolean"));
Col.SetOrdinal(0);// to put the column in position 0;
仅为了改进Wael的答案并将其放在一行中:
dt.Columns.Add("Better", typeof(Boolean)).SetOrdinal(0);
更新:请注意,在您不需要对DataColumn执行其他操作时,此方法是可行的。Add()方法将返回所添加的列,而SetOrdinal()方法将不返回任何内容。
//Example to define how to do :
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Address");
dt.Columns.Add("City");
// The table structure is:
//ID FirstName LastName Address City
//Now we want to add a PhoneNo column after the LastName column. For this we use the
//SetOrdinal function, as iin:
dt.Columns.Add("PhoneNo").SetOrdinal(3);
//3 is the position number and positions start from 0.`enter code here`
//Now the table structure will be:
// ID FirstName LastName PhoneNo Address City