简体   繁体   English

如何从 DataTable 对象知道行索引

[英]How to know the row index from DataTable object

I'm getting a value from DataGridView, and based on particular I want to know its row index using DataTable object.我从 DataGridView 获取一个值,并且基于特定的我想知道它使用 DataTable 对象的行索引。 For instance, if I get the value "this", then I want to know its index in table.例如,如果我得到值“this”,那么我想知道它在表中的索引。 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.如果该值“this”属于DataTableNon-Primary-Key Column ,则可能会返回多行。

To find a value in DataTable , use DataTable 's Select() method:要在DataTable查找值,请使用DataTableSelect()方法:

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

Once you get the row(s), you can find its index using DataTable.Rows.IndexOf() method.获得行后,您可以使用DataTable.Rows.IndexOf()方法找到其索引。

I suggest you find a better way to locate your row from DataTable .我建议您找到一种更好的方法来从DataTable定位您的行。 May be look for row using a value that belongs to a Primary Key Column .可能会使用属于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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM