ASP.NET DataSet Filter by Column Values :
Filtering ASP.NET DataSet values at runtime can prevent multiple trips or calls to your Database which improves your web application’s response time and it performs much better. We can filter ASP.NET DataSet using single Column values, multiple column values and also using two dates to view DataSet according to our need. I have already discussed about filtering ASP.NET DataTable for similar results. Below are the syntax and code usage for filtering ASP.NET DataSet accordingly.
My Database Table :
Syntax for ASP.NET DataSet Filter by single Column Value :
dataSetName.Tables[0].DefaultView.RowFilter = "ColumnName ='YourDataToFilter'"; dataGrid.DataSource = (dataSetName.Tables[0].DefaultView).ToTable(); dataGrid.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); DataSet dsCountries = new DataSet(); sqlAdapter.Fill(dsCountries, "tblCountries"); dsCountries.Tables[0].DefaultView.RowFilter = "Country ='Chile'"; dataGrid.DataSource = (dsCountries.Tables[0].DefaultView).ToTable(); dataGrid.DataBind(); sqlAdapter.Dispose(); cmd.Dispose(); con.Close(); }
Output :
Syntax for ASP.NET DataSet Filter by multiple Column Values :
dataSetName.Tables[0].DefaultView.RowFilter = "ColumnName ='YourDataToFilter' and SecondColumnName ='YourDataToFilter'"; dataGrid.DataSource = (dataSetName.Tables[0].DefaultView).ToTable(); dataGrid.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); DataSet dsCountries = new DataSet(); sqlAdapter.Fill(dsCountries, "tblCountries"); dsCountries.Tables[0].DefaultView.RowFilter = "Country ='India' and Id='1'"; dataGrid.DataSource = (dsCountries.Tables[0].DefaultView).ToTable(); dataGrid.DataBind(); sqlAdapter.Dispose(); cmd.Dispose(); con.Close(); }
Output :
Syntax for ASP.NET DataSet Filter by Date Values :
dataSetName.Tables[0].DefaultView.RowFilter = "DateColumn >= 'FromDate' and DateColumn <='ToDate"; dataGrid.DataSource = (dataSetName.Tables[0].DefaultView).ToTable(); dataGrid.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); DataSet dsCountries = new DataSet(); sqlAdapter.Fill(dsCountries, "tblCountries"); dsCountries.Tables[0].DefaultView.RowFilter = "OnDate >= '2018-02-01 00:00:00.000' and OnDate <='2018-02-05 23:59:59.000'"; dataGrid.DataSource = (dsCountries.Tables[0].DefaultView).ToTable(); dataGrid.DataBind(); sqlAdapter.Dispose(); cmd.Dispose(); con.Close(); }
Output :
Also see :
ASP.NET DataSet Filter using multiple Columns values.
ASP.NET DataSet Filter using Date values.
ASP.NET DataTable – Filtering Data using columns values.
ASP.NET DataTable – Filtering Data using Multiple column values.
ASP.NET DataTable – Filtering Data using Date values.
Pingback: ASP.NET - DataSet Filter using Multiple Column Values • ParallelCodes();
Pingback: ASP.NET - DataSet Filter by Date • ParallelCodes();