search in datagridview C# winfom

后端 未结 3 1592
小蘑菇
小蘑菇 2021-01-06 11:34

I want to put a search option in DataGridView, i.e., User types the string or int in TextBox and similar records the DataGridView shou

相关标签:
3条回答
  • 2021-01-06 12:05
            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 : name of textbox for search word in datagridview1 and
    • txtsearchgroup_KeyUp : event of keyup for search and filter word in datagridview1 and
    • select name from groupitems : name is field for groupitems table and
    • Dv.Table = objdataset.Tables[0] : zero (0) is first table in dataset
    0 讨论(0)
  • 2021-01-06 12:06

    Use the DataView.RowFilter property.

    Also take a look at DataTable.DefaultView

    Take a look at this article I wrote some time back on filtering data real time.

    0 讨论(0)
  • 2021-01-06 12:18

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

    Create a BindingSource and make BindingSource.DataSource the Datatable or DataView that your DGV is currently using. Then set your DataGridView.DataSource to the BindingSource. Then you can use the BindingSource.Filter property to query your datasource by setting the BindingSource.Filterto your query string which will automatically filter the DGV. You can find the syntax here - it is very similar to basic SQL queries, except you can only use wild cards on the beginning and end of the string.

    0 讨论(0)
提交回复
热议问题