如何动态填充DataTable中的行?

时间:2015-06-29 16:52:23

标签: c# .net datatable

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中的行?

1 个答案:

答案 0 :(得分:0)

这是一种方式:

    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"      }
);