62

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?

Termininja
  • 6,620
  • 12
  • 48
  • 49
JOE SKEET
  • 7,950
  • 14
  • 48
  • 64

7 Answers7

120
int index = dt.Rows.IndexOf(row);

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

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • 6
    This code will force a search of the internal Red-Black Tree for each row in the DataTable whereas a simple for loop requires none of that overhead (since you have the index up front). – Justin Niessner Dec 21 '10 at 20:14
  • 7
    Using a for loop is of course a much better choice but that wasn't the question. – Jamie Ide Dec 21 '10 at 21:38
  • Internally if you put a breakpoint on a foreach loop of DataRows you can see the row._rowID but for whatever reason that's not a publicly exposed field. BUt if you are just running code in VS you can set the breakpoint and see this variable to figure out where you are with the loop. But as said it's best to do a for loop and measure the incrementer. – Rob Dec 17 '22 at 14:15
33

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];
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
4

Why don't you try this

for(int i=0; i < dt.Rows.Count; i++)
{
  // u can use here the i
}
ElMaquinista
  • 168
  • 1
  • 11
namco
  • 6,208
  • 20
  • 59
  • 83
4

You have two options here.

  1. You can create your own index counter and increment it
  2. Rather than using a foreach loop, you can use a for loop

The individual row simply represents data, so it will not know what row it is located in.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
3

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.

int i = 0;
int index = 0;
foreach (DataRow row in dt.Rows) 
{
index = i;
// do stuff
i++;
} 
Security Hound
  • 2,577
  • 3
  • 25
  • 42
  • 1
    This answer is hard to decipher, particularly in light of the current question. He's simply asking how to obtain the index of the current row. Is he on row 1, row 2, etc. – Anthony Pegram Dec 21 '10 at 19:19
  • 1
    your original answer is not the same as this one, and it totally missed the mark. If not, you would not have completely replaced it. As for providing my own answer, it's unnecessary. Justin has already provided the answer I would give.* – Anthony Pegram Dec 21 '10 at 21:35
0
ArrayList check = new ArrayList();            

for (int i = 0; i < oDS.Tables[0].Rows.Count; i++)
{
    int iValue = Convert.ToInt32(oDS.Tables[0].Rows[i][3].ToString());
    check.Add(iValue);

}
Marijn
  • 10,367
  • 5
  • 59
  • 80
-1

Try:

int i = Convert.ToInt32(dt.Rows.Count);

I think it's the shortest, thus the simplest way.

Vakho Akobia
  • 304
  • 1
  • 3
  • 13