如何从 DataTable 对象知道行索引 [英] How to know the row index from DataTable object

查看:32
本文介绍了如何从 DataTable 对象知道行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 DataGridView 获取一个值,并且基于特定的我想知道它使用 DataTable 对象的行索引.例如,如果我得到值this",那么我想知道它在表中的索引.我可以知道我该怎么做吗

I'm getting a value from DataGridView, and based on 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

推荐答案

如果该值this"属于 DataTable 中的 Non-Primary-Key Column,您可能会返回多于一行.

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

要在 DataTable 中查找值,请使用 DataTableSelect() 方法:

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

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

获得行后,您可以使用 DataTable.Rows.IndexOf() 方法找到其索引.

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

我建议您找到一种更好的方法来从 DataTable 中定位您的行.可能会使用属于 Primary Key Column 的值查找行.

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.

这篇关于如何从 DataTable 对象知道行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆