Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. For instance, if I get the value "this", then I want to know its index in table. May I know how should I done

like image 281
Justin Avatar asked Dec 24 '10 09:12

Justin


People also ask

How do I get a DataRow index?

Hi every one. DataRow[] dr = dt1. Select("name='" + result[k2] + "' and school='" + result1[k3] + "'");.

What is the row index?

Definition and Usage The rowIndex property returns the position of a row in the rows collection of a table.


2 Answers

DataRow[] result = tableName.Select("Group >= 'Commentary - Yes'");
  if (result.Length > 0)
     {
          int SelectedIndex =tableName.Rows.IndexOf(result[0]);
     }
like image 36
chevhfghfghfgh Avatar answered Oct 17 '22 04:10

chevhfghfghfgh


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.

like image 104
decyclone Avatar answered Oct 17 '22 02:10

decyclone