DataTable.AddNew()

  • Thread starter Thread starter JJ
  • Start date Start date
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
 
rowNew is simply a pointer. Hold onto that in a module level collection or
arraylist.
 
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
 
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.
 
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.
 
Back
Top