There has to be an easy answer: I want to loop through multiple datatables... example:
for (int i = 1 ; i < 7 ; i ++ )
{
DataTable dt = (DataTable) "dt" + i.toString();
// do something with DataTable dt1 as DataTable
// ie开发者_JAVA百科...
int x = dt.Rows.Count
}
The above code obviously does not work.. but how do you find a DataTable like you do FindControl with WebControls?? I can't figure it out.
Uhm, do you have
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();
in your project?
You should then create a list of 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])
Then you can loop like this:
foreach (DataTable dt in datatables)
{
// do something with DataTable dt as DataTable
// ie...
int x = dt.Rows.Count
}
精彩评论