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?
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?
Try:
I think it's the shortest, thus the simplest way.
If you need the index of the item you're working with then using a
foreach
loop is the wrong method of iterating over the collection. Change the way you're looping so you have the index:You do know that DataRow is the row of a DataTable correct?
What you currently have already loop through each row. You just have to keep track of how many rows there are in order to get the current row.
But you're probably better off using a for loop instead of foreach.
Why don't you try this