Thursday, February 11, 2010

Search in Datagridview


                 This is a sample for search datagridview for example.  The user will  type the employee number into the textbox and click on the search button.  then we want the focus to move to the row with the employee number .
private void SelectGridRow(int id)
{
 foreach (DataGridViewRow row in dataGridView1.Rows)
 {
  //assume col 0 as emp column
  if (dataGridView1.Rows[row.Index].Cells[0].Value.Equals(id))
  {
   dataGridView1.Rows[row.Index].Selected = true;
   dataGridView1.Rows[row.Index].DefaultCellStyle.BackColor = Color.Coral;
   dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
   return;
  }
 }

No comments: