如何动态填充DataTable中的行
本文关键字:DataTable 填充 何动态 动态 | 更新日期: 2023-09-27 18:00:18
DataTable exportTable = new DataTable();
exportTable.Columns.Add("Place", typeof(string));
exportTable.Columns.Add("Day", typeof(string));
foreach (var time in timeList)
{
exportTable.Columns.Add(time, typeof(string));
}
exportTable.Rows.Add("New York", 1/1/2015, ?, ?, ?, ...);
如何动态填充DataTable中的一行?
这里有一种方法:
DataTable ItemTable = new DataTable("MyTable");
ItemTable.Columns.Add("ID" , typeof(int ));
ItemTable.Columns.Add("ParentID", typeof(int ));
ItemTable.Columns.Add("Name" , typeof(String));
// add some test data
ItemTable.Rows.Add(new object[] { 0,-1, "Bill Gates" });
ItemTable.Rows.Add(new object[] { 1, 0, "Steve Ballmer" });
ItemTable.Rows.Add(new object[] { 3, 1, "Mary Smith" });
ItemTable.Rows.Add(new object[] { 2, 0, "Paul Allen" });
ItemTable.Rows.Add(new object[] { 4, 2, "Ahmed Jones" });
ItemTable.Rows.Add(new object[] { 5, 2, "Wing Lee" }
);