ASP.NET – DataTable Filter by Multiple Column Values
Filtering Data of ASP.NET DataTable can avoid round trips to your database multiple times and also increases your Web application’s response time. Filtering the values of ASP.NET DataTable can be achieved using RowFilter property where in we can make use of “where” clause to filter our DataTable based on Column values. Filtering can also be done for Date values using the ‘between’ keyword. Below are the Syntax and usage of the Syntax respectively. ASP.NET datatable filter :
My Database table :
Syntax for filtering ASP.NET DataTable using multiple column values :
DataView dv = dataTableId.DefaultView; dv.RowFilter = "FirstColumnName ='YourFilteringData' and SecondColumnName ='YourFilteringData' "; dataGridId.DataSource = dv; dataGridId.DataBind();
Usage :
protected void Page_Load(object sender, EventArgs e) { FillGrid(); } public void FillGrid() { SqlConnection con = new SqlConnection("Data Source=192.168.0.192;Initial Catalog=ParallelCodes;User ID=sa;Password=789"); SqlCommand cmd = new SqlCommand("Select * from tblCountries", con); SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); DataTable dtCountries = new DataTable(); sqlAdapter.Fill(dtCountries); DataView dv = dtCountries.DefaultView; dv.RowFilter = "Country ='India' and Id='1'"; dataGrid.DataSource = dv; dataGrid.DataBind(); sqlAdapter.Dispose(); cmd.Dispose(); con.Close(); }
Output :
Also see :
ASP.NET DataTable filter by Date values – Filtering data of DataTable in ASP.NET using dates.
ASP.NET DataTable filter – Filtering data of DataTable in ASP.NET.