How to know the row index from DataTable object

前端 未结 3 1713
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 09:07

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 t

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 09:17

    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.

提交回复
热议问题