通过DataTable.Id帮助查找DataTable!

时间:2010-11-14 20:41:38

标签: c# asp.net datatable

必须有一个简单的答案: 我想循环遍历多个数据表...... 例如:

for (int i = 1 ; i < 7 ; i ++ )
{
    DataTable dt = (DataTable) "dt" + i.toString();

    // do something with DataTable dt1 as DataTable
    //  ie...

    int x = dt.Rows.Count
}

上面的代码显然不起作用..但你怎么找到像你用WebControls做FindControl的DataTable?我无法理解。

1 个答案:

答案 0 :(得分:0)

嗯,你有吗

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
DataTable dt4 = new DataTable();
DataTable dt5 = new DataTable();
DataTable dt6 = new DataTable();
DataTable dt7 = new DataTable();

在你的项目中?

然后,您应该创建一个DataTables列表:

List<DataTable> datatables = new List<DataTable>();

datatables.Add(new DataTable());  // dt1 (now datatables[0])
datatables.Add(new DataTable());  // dt2 (now datatables[1])
datatables.Add(new DataTable());  // dt3 (now datatables[2])
datatables.Add(new DataTable());  // dt4 (now datatables[3])
datatables.Add(new DataTable());  // dt5 (now datatables[4])
datatables.Add(new DataTable());  // dt6 (now datatables[5])
datatables.Add(new DataTable());  // dt7 (now datatables[6])

然后你可以像这样循环:

foreach (DataTable dt in datatables)
{
    // do something with DataTable dt as DataTable
    //  ie...

    int x = dt.Rows.Count    
}