Luiscencio
Luiscencio

Reputation: 3965

How can I know a row index while iterating with foreach?

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

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

Upvotes: 11

Views: 76688

Answers (13)

Wei Chun
Wei Chun

Reputation: 1343

The alternative way to retrieve data by using index instead of using column name

foreach (DataRow temprow in temptable.Rows)
{
    String col1 = temprow[0].ToString().Trim();
    String col2 = temprow[1].ToString().Trim();
}

Hope it help

Upvotes: 0

Mehmet Ali
Mehmet Ali

Reputation: 11

Write any Cell number and get RowIndex


foreach (var item in datagridview.Rows)
   {
     //TextBox1.Text= item.Cells[0].RowIndex.ToString();
   }

Upvotes: 1

Carpentweet
Carpentweet

Reputation: 349

Better late than never...

foreach (DataRow temprow in temptable.Rows)
{
    temptable.Rows.IndexOf(temprow);
}

Upvotes: 1

toddmo
toddmo

Reputation: 22416

Hey there's a much faster way I think. No iteration required! First, declare a static variable for the Friend RowID Field of the DataRow:

Private Shared RowIDFieldInfo As System.Reflection.FieldInfo = GetType(DataRow).GetField("_rowID", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)

Then All you need to do to use it is:

RowIDFieldInfo.GetValue(MyDataRow) - 1

I have not tested this after resorting or filtering. In my case I haven't a need to do that, so this works.

Upvotes: 1

adrianbanks
adrianbanks

Reputation: 82944

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;
}

Upvotes: 16

Jon Skeet
Jon Skeet

Reputation: 1500615

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;
}

Upvotes: 17

Joseph
Joseph

Reputation: 25523

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.

Upvotes: 23

Yoopergeek
Yoopergeek

Reputation: 5642

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.

Upvotes: 0

bendewey
bendewey

Reputation: 40235

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];
}

Upvotes: 0

JaredPar
JaredPar

Reputation: 754753

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) 
{
  ...
});

Upvotes: 1

Filip Ekberg
Filip Ekberg

Reputation: 36287

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++;
}

Upvotes: 6

Erich
Erich

Reputation: 3972

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;
}

Upvotes: 0

Nick DeVore
Nick DeVore

Reputation: 10166

int rowIndex = temptable.Rows.IndexOf(temprow);

Upvotes: 5

Related Questions