Can anyone help me please?
I have to find the row number from 1 selected array which I stored in one separate array and randomly I'm trying to get the row number from datagridview
In other words: if I know a column value for a given row in a datagridview (e.g. for this row, FirstName == 'Bud'), how can I get the row ID?
You can use 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;
There's probably some easier way where you filter it in some way but at the moment I can only think of looping 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With