Find a row in dataGridView based on column and value

前端 未结 7 1463
轻奢々
轻奢々 2020-12-07 14:54

I have a dataGridView that has 3 columns: SystemId, FirstName, LastName that is bound using database information. I would like to highlight a certain row, which I would do

7条回答
  •  情书的邮戳
    2020-12-07 15:21

    Those who use WPF

    for (int i = 0; i < dataGridName.Items.Count; i++)
    {
          string cellValue= ((DataRowView)dataGridName.Items[i]).Row["columnName"].ToString();                
          if (cellValue.Equals("Search_string")) // check the search_string is present in the row of ColumnName
          {
             object item = dataGridName.Items[i];
             dataGridName.SelectedItem = item; // selecting the row of dataGridName
             dataGridName.ScrollIntoView(item);                    
             break;
          }
    }
    

    if you want to get the selected row items after this, the follwing code snippet is helpful

    DataRowView drv = dataGridName.SelectedItem as DataRowView;
    DataRow dr = drv.Row;
    string item1= Convert.ToString(dr.ItemArray[0]);// get the first column value from selected row 
    string item2= Convert.ToString(dr.ItemArray[1]);// get the second column value from selected row 
    

提交回复
热议问题