Row number in DataRow.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I am iterating through the rows in a table through foreach statement. I am
using DataRow object here to store the row from the DataRow collection.
Within the loop I need to find out the current row number. I could have used
an integer variable outside the loop and used it as counter. I wanted to know
if there is any better way of doing it, perhaps using some properties of
DataRow object itself.

foreach (DataRow row in dataset.Tables[0].Rows)
{
//I want to find the current row number here;
}

Thanks
pradeep
 
This is not possible. A DataRow does not know its index in the collection.
You would have to figure it out by looping through the rows, comparing each
row to the current row to see if it's it. Obviously this is very slow. I
recommend you use a for loop instead of a for each loop to avoid having to
do this.
 
Thank you Marina for your suggestion..

I have found the solution myself. Here is the code that I wrote

int rowNum;
foreach (DataRow row in dataset.Tables[0].Rows)
{
rowNum = dataset.Tables[0].Rows.IndexOf(row);
}

- pradeep

Marina Levit said:
This is not possible. A DataRow does not know its index in the collection.
You would have to figure it out by looping through the rows, comparing each
row to the current row to see if it's it. Obviously this is very slow. I
recommend you use a for loop instead of a for each loop to avoid having to
do this.

pradeep_TP said:
Hi All,

I am iterating through the rows in a table through foreach statement. I am
using DataRow object here to store the row from the DataRow collection.
Within the loop I need to find out the current row number. I could have
used
an integer variable outside the loop and used it as counter. I wanted to
know
if there is any better way of doing it, perhaps using some properties of
DataRow object itself.

foreach (DataRow row in dataset.Tables[0].Rows)
{
//I want to find the current row number here;
}

Thanks
pradeep
 
This looks like a new method in 2.0, was not there in previous versions.

However, using a for loop will still be faster then asking the rows
collection to figure out the row number for every row.

pradeep_TP said:
Thank you Marina for your suggestion..

I have found the solution myself. Here is the code that I wrote

int rowNum;
foreach (DataRow row in dataset.Tables[0].Rows)
{
rowNum = dataset.Tables[0].Rows.IndexOf(row);
}

- pradeep

Marina Levit said:
This is not possible. A DataRow does not know its index in the
collection.
You would have to figure it out by looping through the rows, comparing
each
row to the current row to see if it's it. Obviously this is very slow.
I
recommend you use a for loop instead of a for each loop to avoid having
to
do this.

pradeep_TP said:
Hi All,

I am iterating through the rows in a table through foreach statement. I
am
using DataRow object here to store the row from the DataRow collection.
Within the loop I need to find out the current row number. I could have
used
an integer variable outside the loop and used it as counter. I wanted
to
know
if there is any better way of doing it, perhaps using some properties
of
DataRow object itself.

foreach (DataRow row in dataset.Tables[0].Rows)
{
//I want to find the current row number here;
}

Thanks
pradeep
 
Back
Top