How to get the row number from a datatable?

JOE SKEET picture JOE SKEET · Dec 21, 2010 · Viewed 189.9k times · Source

I am looping through every row in a datatable:

foreach (DataRow row in dt.Rows) {}

I would like to get the index of the current row within the dt datatable. for example:

int index = dt.Rows[current row number]

How do i do this?

Answer

Jamie Ide picture Jamie Ide · Dec 21, 2010
int index = dt.Rows.IndexOf(row);

But you're probably better off using a for loop instead of foreach.