Reputation: 3564
Is it possible to create (allocate memory) a DataTable array and then access it like that for instance:
dt[0].NewRow();
If this is possible how can I allocate memory?
Thanks, Sun
ps I m using C# 2.0
Upvotes: 4
Views: 29264
Reputation: 57220
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
Upvotes: 6
Reputation: 2845
Try this:
DataRow dr = dt.NewRow();
dr[0] = "Sydney"; // or you could generate some random string.
dt.Rows.Add(dr);
Upvotes: 2
Reputation: 11820
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();
Upvotes: 9