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
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(); }
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.
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.Filter
to 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.