How to get the row number from a datatable?

前端 未结 7 1430
谎友^
谎友^ 2020-12-08 06:18

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

7条回答
  •  死守一世寂寞
    2020-12-08 07:04

    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:

    for(int i = 0; i < dt.Rows.Count; i++)
    {
        // your index is in i
        var row = dt.Rows[i];
    }
    

提交回复
热议问题