Search in datagridview c # winfom

I want to put the search option in DataGridView, i.e. the user enters a string or int in TextBoxand similar entries DataGridView. I tried to use this event KeyPressbut did not work.

  if (Char.IsLetter(e.KeyChar))
  {
     for (int i = 0; i < (dgemployee.Rows.Count); i++)
     {
         if (dgemployee.Rows[i].Cells["Employee"].Value.ToString().
                    StartsWith(e.KeyChar.ToString(), true, 
                    CultureInfo.InvariantCulture))
         {
             dgemployee.Rows[i].Cells[0].Selected = true;
             return;                     
         }
      }

Actually, I need to search for everything DataGridView, not just one column and row. So, any better solution?

+3
source share
3 answers

If your DataGridView is bound to a DataTable or DataView, you can do this:

BindingSource BindingSource.DataSource Datatable DataView, DGV. DataGridView.DataSource BindingSource. BindingSource.Filter , BindingSource.Filter , DGV. - SQL-, , .

+5
        private void txtsearchgroup_KeyUp(object sender, KeyEventArgs e) {
        SqlConnection objconnection = new SqlConnection(servername and ...);
        DataView Dv = new DataView();
        objcommand = new SqlCommand("select name from groupitems", objconnection);
        objdataadapter = new SqlDataAdapter();
        objdataadapter.SelectCommand = new SqlCommand();
        objdataadapter.SelectCommand = objcommand;
        objdataset = new DataSet();
        objconnection.Open();
        objdataadapter.Fill(objdataset);
        Dv.Table = objdataset.Tables[0];
        Dv.RowFilter = " name LIKE '%" + txtsearchgroup.Text + "%'";
        dataGridView1.DataSource = Dv;
        objconnection.Close(); }
  • txtsearchgroup: datagridview1
  • txtsearchgroup_KeyUp: keyup datagridview1
  • select a name from groupitems: name is a field for the groupitems table and
  • Dv.Table = objdataset.Tables [0]: zero (0) - the first table in the data set
0
source

All Articles