Home All Groups Group Topic Archive Search About

Row index property in DataTable.Rows collection?

Author
28 Feb 2006 9:38 PM
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

Author
28 Feb 2006 10:35 PM
Jim Hughes
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];
   /// ...
}

"Bradley" <annonym***@ms.com> wrote in message
news:%23POfL9KPGHA.1532@TK2MSFTNGP12.phx.gbl...
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
Author
1 Mar 2006 6:42 AM
Aboulfazl Hadi
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
Author
1 Mar 2006 10:12 PM
Bradley
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

Show quote
"Aboulfazl Hadi" <AHa***@gmail.com> wrote in message
news:1141195332.744966.144100@t39g2000cwt.googlegroups.com...
> 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
>

AddThis Social Bookmark Button