DataTable.AddNew()

J

JJ

Hello NG

I have the following code:

DataTable dt = new DataTable();
..... prepare the columns.
DataRow rowNew = dt.NewRow();
....
....
DataTable.Rows.Add(rowNew);

My question, can i be sure that datatable.rows.add() method always adds to
end of rows. So that if i need to get the row again, i can simply use the
rowNew = dt.Rows[dt.Rows.Count-1];

I know that I can drill the rows to locate the rowNew.RowState = Added but
if I have more than one added row i can't use that.

Kind regards.
Johnny E. Jensen
 
J

Jim Rand

rowNew is simply a pointer. Hold onto that in a module level collection or
arraylist.
 
J

JJ

Eh...
That was'nt the question.
If i had the ability to just hold on to the rowNew obejct, why ask for
finding it in the datatable.rows collection?
I want to ensure that ...rows.add method i always adding row to the end of
the rows collection.

Kind regards
JJ

Jim Rand said:
rowNew is simply a pointer. Hold onto that in a module level collection or
arraylist.

JJ said:
Hello NG

I have the following code:

DataTable dt = new DataTable();
.... prepare the columns.
DataRow rowNew = dt.NewRow();
...
...
DataTable.Rows.Add(rowNew);

My question, can i be sure that datatable.rows.add() method always adds
to end of rows. So that if i need to get the row again, i can simply use
the rowNew = dt.Rows[dt.Rows.Count-1];

I know that I can drill the rows to locate the rowNew.RowState = Added
but if I have more than one added row i can't use that.

Kind regards.
Johnny E. Jensen
 
M

Miha Markic

I think it is always added to the end of the collection.
However, if you want to be on the safe side then you should search rows
using unique key.
 
S

Scott M.

It always adds to the end. But, be carefull that when you go to get that
"last row", that another row hasn't been added in the interim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top