Sunscreen
Sunscreen

Reputation: 3564

DataTable array

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

Answers (3)

digEmAll
digEmAll

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

asma
asma

Reputation: 2845

Try this:

DataRow dr = dt.NewRow();
dr[0] = "Sydney"; // or you could generate some random string.
dt.Rows.Add(dr);

Upvotes: 2

Renatas M.
Renatas M.

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

Related Questions