Find a row in dataGridView based on column and value

前端 未结 7 1462
轻奢々
轻奢々 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:27

    Try this:

            string searchValue = textBox3.Text;
            int rowIndex = -1;
    
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            try
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells["peseneli"].Value.ToString().Equals(searchValue))
                    {
                        rowIndex = row.Index;
                        dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
                        dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    
                        break;
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
    

提交回复
热议问题