Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet ado.net > february 2006 >

dotnet ado.net : Row index property in DataTable.Rows collection?


Jim Hughes
2/28/2006 2:35:31 PM
If the index matters to you, iterate by the index instead of using foreach.

for (int myIndex=0;myIndex < myDataTable.Rows;myIndex++)
{
DataRow myDataRow = myDataTable.Rows[myIndex];
/// ...
}

[quoted text, click to view]
Hi,
I understand that a DataTable has a collection of Rows. If it is a
collection, why is there no obvious way to get an index property from any
given row?

Example code:

....
int myIndex;

foreach (DataRow myDataRow in myDataTable.Rows)
{
// I need to get the zero-based index here:
myIndex = myDataRow.index?
....
}
....

I don't know if an iEnumerator (or something like it) is required, or how to
use it because of my lack of experience in this area. Can anyone help?
Thanks! Bradley

Bradley
2/28/2006 10:38:15 PM
Hi,=20
I understand that a DataTable has a collection of Rows. If it is a =
collection, why is there no obvious way to get an index property from =
any given row?

Example code:

....
int myIndex;

foreach (DataRow myDataRow in myDataTable.Rows)
{
// I need to get the zero-based index here:
myIndex =3D myDataRow.index?
....
}
....

I don't know if an iEnumerator (or something like it) is required, or =
how to use it because of my lack of experience in this area. Can anyone =
help?
Aboulfazl Hadi
2/28/2006 10:42:12 PM
Hi
If you use .Net Framework 2.0, there is a method IndexOf in Rows
Collection
So for getting index of a row :
DataRow row;
// getting row


// need to row index
int rowIndex=table.Rows.IndexOf(row)


I Hope this helps
A.Hadi
Bradley
3/1/2006 11:12:24 PM
Thank you both. Changing the control loop to "for (int myIndex=0;..." did
the trick since I don't yet have Framework 2.0 installed. Now I'd like to
find out more about the 2.0 framework. I found out that you can install it
in addition to the 1.1 framework but I don't yet know how my C# projects can
be "told" to use 2.0. I guess I will search the framework newsgroup.
Bradley

[quoted text, click to view]

AddThis Social Bookmark Button