In this article I will explain with an example, how to filter DataSet based on Column value using C# and VB.Net.
Actually it’s the DataTable inside the DataSet which will be filtered based on Column value using the DataTable.DefaultView.RowFilter property in C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Filter DataSet based on Column value using C# and VB.Net
 
I have already inserted few records in the table.
Filter DataSet based on Column value using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The following HTML Markup consists of a DropDownList and a GridView. The DropDownList is assigned OnSelectedIndexChanged property and the AutoPostBack property is set to True.
Country:
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Changed">
    <asp:ListItem Text="" Value=""></asp:ListItem>
    <asp:ListItem Text="United States" Value="United States"></asp:ListItem>
    <asp:ListItem Text="India" Value="India"></asp:ListItem>
    <asp:ListItem Text="France" Value="France"></asp:ListItem>
    <asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
</asp:DropDownList>
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Filter DataTable based on Column value using C# and VB.Net
A function named GetData is used to fetch the records from the Customers table and return them in the form of a DataSet.
The GetData function is called two places, first inside the Page_Load event of the page and second inside the Country_Changed event handler which is triggered on the DropDownList SelectedIndexChanged event.
Inside the Country_Changed event handler, first the selected value of the Country is fetched from the DropDownList.
If the selected value is not empty, then the RowFilter property of the DataView of the DataTable present inside the DataSet is set which filters the records in the DataView based on the selected Country.
Finally the filtered DataView is used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataSet ds = this.GetData();
        gvCustomers.DataSource = ds;
        gvCustomers.DataBind();
    }
}
 
protected void Country_Changed(object sender, EventArgs e)
{
    string country = ddlCountry.SelectedItem.Value;
    DataSet ds = this.GetData();
    DataView dataView = ds.Tables[0].DefaultView;
    if (!string.IsNullOrEmpty(country))
    {
        dataView.RowFilter = "Country = '" + country + "'";
    }
    gvCustomers.DataSource = dataView;
    gvCustomers.DataBind();
}
 
private DataSet GetData()
{
    DataSet ds = new DataSet();
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string sql = "SELECT * FROM Customers";
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(sql))
        {
            cmd.Connection = conn;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(ds);
            }
        }
    }
 
    return ds;
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim ds As DataSet = Me.GetData()
        gvCustomers.DataSource = ds
        gvCustomers.DataBind()
    End If
End Sub
 
Protected Sub Country_Changed(sender As Object, e As EventArgs)
    Dim country As String = ddlCountry.SelectedItem.Value
    Dim ds As DataSet = Me.GetData()
    Dim dataView As DataView = ds.Tables(0).DefaultView
    If Not String.IsNullOrEmpty(country) Then
        dataView.RowFilter = "Country = '" & country & "'"
    End If
    gvCustomers.DataSource = dataView
    gvCustomers.DataBind()
End Sub
 
Private Function GetData() As DataSet
    Dim ds As New DataSet()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim sql As String = "SELECT * FROM Customers"
    Using conn As New SqlConnection(constr)
        Using cmd As New SqlCommand(sql)
            cmd.Connection = conn
            Using sda As New SqlDataAdapter(cmd)
                sda.Fill(ds)
            End Using
        End Using
    End Using
 
    Return ds
End Function
 
 
Screenshot
Filter DataSet based on Column value using C# and VB.Net
 
 
Demo
 
 
Downloads