Apr 03

how to filter dataset in asp.net using c#


In this article I will explain you how to set filter in dataset while getting data from backend to frontend.

Observe the below sqlserver database table , In this we can see the pincode column were it contain different values in pincode column


Eg:I want to get only picode=524004 for that I can write condition in database also but in some scenarios like dynamic sp’s, It won’t work at that scenario we can use this filter in code level

1.Add a page FilterInDS.aspx to solution

Copy and paste this Body in that page

[code language=”css”]


<form id="form1" runat="server">


<asp:Button ID="btnGetData" runat="server" Text="GetData" OnClick="btnGetData_Click" />

<asp:GridView ID="gdvData" runat="server">






Copy and paste below code in cs file

[code language=”csharp”]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

public partial class FilterinDS : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)



protected void btnGetData_Click(object sender, EventArgs e)


SqlConnection Connection = new SqlConnection("Server=Naseer-PC;Database=Naseer;Uid=sa;Pwd=123");

DataSet ds = new DataSet();

SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_Mst_EmployeeDetails", Connection);




var strExpr = "PinCode = ‘" + 524004 + "’";

var dv = ds.Tables[0].DefaultView;

dv.RowFilter = strExpr;

var newDataTable = dv.ToTable();

DataSet ds1 = new DataSet();


if (newDataTable.Rows.Count > 0)


gdvData.DataSource = newDataTable;




catch (Exception ex)







Execute the code and you will get filtered data based on pincode as below


Happy Coding!!!!!


1 comment

  1. w45ry


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>