开发者

How to know the row index from DataTable object

开发者 https://www.devze.com 2023-02-01 05:52 出处:网络
I\'m getting a value from DataGridView, and based o开发者_JAVA技巧n particular I want to know its row index using DataTable object. For instance, if I get the value \"this\", then I want to know its i

I'm getting a value from DataGridView, and based o开发者_JAVA技巧n particular I want to know its row index using DataTable object. For instance, if I get the value "this", then I want to know its index in table. May I know how should I done


If that value "this" belongs to a Non-Primary-Key Column in DataTable, you may get more than one rows returned.

To find a value in DataTable, use DataTable's Select() method:

DataRow[] rows = dt.Select("Column1 = 'this'");

Once you get the row(s), you can find its index using DataTable.Rows.IndexOf() method.

I suggest you find a better way to locate your row from DataTable. May be look for row using a value that belongs to a Primary Key Column.

It would be great to know why you want to do this. Someone could come up with a better solution.


DataRow[] result = tableName.Select("Group >= 'Commentary - Yes'");
  if (result.Length > 0)
     {
          int SelectedIndex =tableName.Rows.IndexOf(result[0]);
     }


using System.Data;

DataRowView row1 = (DataRowView)dataGridView1.CurrentRow.DataBoundItem;

int idx11 = row1.Row.Table.Rows.IndexOf(row1.Row);
0

精彩评论

暂无评论...
验证码 换一张
取 消