In this article I will explain with an example, how to filter DataTable based on Column value using C# and VB.Net.
The DataTable 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.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
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 and Bind to GridView in ASP.Net
A function named GetData is used to fetch the records from the Customers table and return them in the form of a DataTable.
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, the RowFilter property of the DataTable’s DataView 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)
{
DataTable dt = this.GetData();
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void Country_Changed(object sender, EventArgs e)
{
string country = ddlCountry.SelectedItem.Value;
DataTable dt = this.GetData();
DataView dataView = dt.DefaultView;
if (!string.IsNullOrEmpty(country))
{
dataView.RowFilter = "Country = '" + country + "'";
}
gvCustomers.DataSource = dataView;
gvCustomers.DataBind();
}
private DataTable GetData()
{
DataTable dt = new DataTable();
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(dt);
}
}
}
return dt;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = Me.GetData()
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub Country_Changed(sender As Object, e As EventArgs)
Dim country As String = ddlCountry.SelectedItem.Value
Dim dt As DataTable = Me.GetData()
Dim dataView As DataView = dt.DefaultView
If Not String.IsNullOrEmpty(country) Then
dataView.RowFilter = "Country = '" & country & "'"
End If
gvCustomers.DataSource = dataView
gvCustomers.DataBind()
End Sub
Private Function GetData() As DataTable
Dim dt As New DataTable()
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(dt)
End Using
End Using
End Using
Return dt
End Function
Screenshot
Demo
Downloads