Is it possible to create (allocate memory) a DataTable array and then access it like that for instance:
dt[0].NewRow();
If this i开发者_Python百科s possible how can I allocate memory?
Thanks, Sun
ps I m using C# 2.0
Why not to use DataSet instead array?
DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables.Add(new DataTable());
ds.Tables[0].NewRow();
Yes, of course you can:
int n = 10; // the number of datatables
DataTable[] dtArray = new DataTable[n];
for(int i=0; i < n; i++)
dtArray[i] = new DataTable("DataTable " + i);
if you don't know the number of DataTables in advance, you can use an expandable structure like a List<DataTable>
:
List<DataTable> dtList = new List<DataTable>();
dtList.Add(new DataTable());
dtList.Add(new DataTable());
...
or, as suggested by Reniuz, you can use a DataSet
Try this:
DataRow dr = dt.NewRow();
dr[0] = "Sydney"; // or you could generate some random string.
dt.Rows.Add(dr);
精彩评论