Row index property in DataTable.Rows collection?

  • Thread starter Thread starter Bradley
  • Start date Start date
B

Bradley

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
 
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];
/// ...
}

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
 
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
 
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
 
Back
Top