How to find row id from datagridview given row value?

Can anybody help me?

I need to find the row number from 1 selected array, which I saved in one separate array, and by chance I try to get the row number from datagridview

In other words: if I know the column value for a given row in a datagridview (for example, for this row, FirstName == 'Bud'), how can I get the row identifier?

+6
c # datagridview
source share
4 answers

You can use the LINQ query:

int index = -1; DataGridViewRow row = dgv.Rows .Cast<DataGridViewRow>() .Where(r => r.Cells[columnId].Value.ToString().Equals("Some string")) .First(); index = row.Index; 
+19
source share

There is probably a slightly simpler way that you somehow filter it, but for now I can only think about going through it.

 int rowIndex = -1; foreach(DataGridViewRow row in DataGridView1.Rows) { if(row.Cells(1).Value.ToString().Equals("mystr")) { rowIndex = row.Index; break; } } // rowIndex is now either the correct index or -1 if not found 
+1
source share

From: http://www.vbforums.com/showthread.php?t=610134

Call the BindingSource search method, and it will return the index of the matching string, if there is one. If there is, you index the BindingSource to get this row and update the corresponding fields. If not, you call the AddNew BindingSource method to create a new row and set the appropriate fields.

0
source share
 //If U Have Add CheckBox To Ur Datagridview int rowIndex = -1; DataGridViewCheckBoxCell oCell; foreach (DataGridViewRow row in dataGridView1.Rows) { oCell = row.Cells[0] as DataGridViewCheckBoxCell; bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value); if (true == bChecked) { rowIndex = row.Index; //ur code } } 
0
source share

All Articles