I have a Datagridview and the Data Source
is dtCustomer
I just want to filter the content of grid view based on a search text.
Itried the follow
You could try using a DataView (code not tested) -
DataView dv = new DataView(dtSearch);
dv.RowFilter = "cust_Name like '" + txtSearch.Text + "%'";
grvCustomer.DataSource = dv;
Or Try this;
dataGridView.Datasource = datatable.Select("....").CopyToDataTable()
I think this is what you're looking for?
//DataTable dtSearch = dtCustomer;
//dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
grvCustomer.DataSource = dtCustomer.Select("cust_Name like '" + txtSearch.Text + "%'");
And when you want to go back to the original data
grvCustomer.DataSource = dtCustomer;
dtCustomer.Rows.Cast<DataRow>().Select(dr => (string)dr["cust_Name"].Startswith("zzz")).ToList()
The return value for DataTable.Select is a DataRow[] array. It returns a list of matching DataRows. Your code does nothing with those rows at the moment.
You can setup a DataView with a filter and set the grid's DataSource to the DataView:
DataView dv = new DataView(dtSearch);
dv.RowFilter = "...";
grvCustomer.DataSource = dv;
DataTable.Select returns array of row, but you are binding entire data table not filtered rows. use this way or DataView
DataTable dtSearch = dtCustomer;
var filter = dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
grvCustomer.DataSource = filter.ToList();