tags:

views:

406

answers:

9

in the next example how can I know the current row index?

foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
}
+7  A: 

You have to create one yourself

var i = 0;
foreach (DataRow temprow in temptable.Rows)
{
    this.text = i;
    // etc
    i++;
}

or you can just do a for loop instead.

Joseph
+1, though I would rarely (if ever) advocate a `for` instead of a `foreach`. Different storage mechanisms can perform very poorly on index-based lookups (a linked list, for instance) and very well on a straight enumeration.
Adam Robinson
@Adam Understandable, measuring is probably the best thing to figure out which way would be better.
Joseph
A: 
int rowIndex = temptable.Rows.IndexOf(temprow);
Nick DeVore
I would advice against that. It would probably cost a lot more than using your own incrementer.
Filip Ekberg
A: 

Either use a for-loop, or use an integer follow along:

int count =0;
foreach (DataRow temprow in temptable.Rows)
{
    //count is the index of the row in the array temptable.Rows
    //this.text = temprow.INDEX????
    ++count;
}
Erich
+2  A: 

You actually Don't. One of the beauties with foreach is that you don't have the extra set of code handling incrementing and checks on the length.

If you want to have your own Index you would have to do something like this

int rowindex = 0;
foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
    this.text = rowindex++;
}
Filip Ekberg
+1  A: 

It's not possible with a standard foreach loop. The simplest way is to use a for loop

for ( int i = 0; i < temptable.Rows.Count; i++ ) {
  DataRow temprow = (DataRow)temptable.Rows[i];
  ...
}

Another option is to use an extension method

public static void ForEachIndex<T>(this IEnumerable<T> e, Action<T,int> del) {
  var i = 0; 
  foreach ( var cur in e ) {
    del(cur,i);
  }
}

...

temptable.Rows.Cast<DataRow>.ForEachIndex((cur,index) 
{
  ...
});
JaredPar
A: 

You can use the standard for loop to get the index

for(int i=0; i<temptable.Rows.Count; i++)
{
   var index = i;
   var row = temptable.Rows[i];
}
bendewey
A: 

While LFSR's answer is right, I'm pretty sure calling .IndexOf on just about any collection/list is going to enumerate the list until it finds a the matching row. For large DataTable's this could be slow.

It might be better to for (i = 0; i < temptable.Rows.Count; i++) { ... } over the table. That way you have the index without imposing a find-the-index tax.

Yoopergeek
+5  A: 

I have a type in MiscUtil which can help with this - SmartEnumerable. It's a dumb name, but it works :) See the usage page for details, and if you're using C# 3 you can make it even simpler:

foreach (var item in temptable.Rows.AsSmartEnumerable())
{
    int index = item.Index;
    DataRow value = item.Value;
    bool isFirst = item.IsFirst;
    bool isLast = item.IsLast;
}
Jon Skeet
+1  A: 

If you can use Linq, you can do it this way:

foreach (var pair in temptable.Rows.Cast<DataRow>()
                                   .Select((r, i) => new {Row = r, Index = i}))
{
    int index = pair.Index;
    DataRow row = pair.Row;
}
adrianbanks
Man, I was just going to enter this response... oh well
Matthew Whited