Hi roon3y,
Please refer below sample.
HTML
City :
<asp:DropDownList ID="ddlCity" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Select" Value="0" />
</asp:DropDownList>
Country:
<asp:DropDownList ID="ddlCountry" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Select" Value="0" />
</asp:DropDownList>
<asp:Button ID="btnSearch" runat="server" OnClick="Search" Text="Search" />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" AllowPaging="true"
AllowSorting="true" OnSorting="OnSorting" OnPageIndexChanging="OnPageIndexChanging"
PageSize="2">
<Columns>
<asp:TemplateField HeaderText="CustomerID" SortExpression="CustomerID">
<ItemTemplate>
<%# Eval("CustomerID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
private string SortDirection
{
get { return ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC"; }
set { ViewState["SortDirection"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid(string sortExpression = null, string searchCity = null, string searchCountry = null)
{
string query = "SELECT CustomerId, ContactName, City, Country FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
if (!string.IsNullOrEmpty(searchCity) && !string.IsNullOrEmpty(searchCountry))
{
query += " WHERE City = @City AND Country=@Country ";
cmd.Parameters.AddWithValue("@City", searchCity);
cmd.Parameters.AddWithValue("@Country", searchCountry);
}
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandText = query;
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
if (sortExpression != null)
{
DataView dv = dt.AsDataView();
this.SortDirection = this.SortDirection == "ASC" ? "DESC" : "ASC";
dv.Sort = sortExpression + " " + this.SortDirection;
this.gvCustomers.DataSource = dv;
}
else
{
ddlCity.DataSource = dt;
ddlCountry.DataSource = dt;
this.gvCustomers.DataSource = dt;
ddlCity.DataValueField = "City";
ddlCity.DataTextField = "City";
ddlCountry.DataValueField = "Country";
ddlCountry.DataTextField = "Country";
}
ddlCity.DataBind();
ddlCountry.DataBind();
this.gvCustomers.DataBind();
}
}
}
}
}
protected void Search(object sender, EventArgs e)
{
this.BindGrid(null, ddlCity.SelectedItem.Value, ddlCountry.SelectedItem.Value);
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvCustomers.PageIndex = e.NewPageIndex;
this.BindGrid(null, ddlCity.SelectedItem.Value, ddlCountry.SelectedItem.Value);
}
protected void OnSorting(object sender, GridViewSortEventArgs e)
{
this.BindGrid(e.SortExpression, ddlCity.SelectedItem.Value, ddlCountry.SelectedItem.Value);
}
VB.Net
Private Property SortDirection As String
Get
Return If(ViewState("SortDirection") IsNot Nothing, ViewState("SortDirection").ToString(), "ASC")
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid(Optional ByVal sortExpression As String = Nothing, Optional ByVal searchCity As String = Nothing, Optional ByVal searchCountry As String = Nothing)
Dim query As String = "SELECT CustomerId, ContactName, City, Country FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("Constring").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
If Not String.IsNullOrEmpty(searchCity) AndAlso Not String.IsNullOrEmpty(searchCountry) Then
query += " WHERE City = @City AND Country=@Country "
cmd.Parameters.AddWithValue("@City", searchCity)
cmd.Parameters.AddWithValue("@Country", searchCountry)
End If
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandText = query
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
If sortExpression IsNot Nothing Then
Dim dv As DataView = dt.AsDataView()
Me.SortDirection = If(Me.SortDirection = "ASC", "DESC", "ASC")
dv.Sort = sortExpression & " " & Me.SortDirection
Me.gvCustomers.DataSource = dv
Else
ddlCity.DataSource = dt
ddlCountry.DataSource = dt
Me.gvCustomers.DataSource = dt
ddlCity.DataValueField = "City"
ddlCity.DataTextField = "City"
ddlCountry.DataValueField = "Country"
ddlCountry.DataTextField = "Country"
End If
ddlCity.DataBind()
ddlCountry.DataBind()
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGrid(Nothing, ddlCity.SelectedItem.Value, ddlCountry.SelectedItem.Value)
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
Me.gvCustomers.PageIndex = e.NewPageIndex
Me.BindGrid(Nothing, ddlCity.SelectedItem.Value, ddlCountry.SelectedItem.Value)
End Sub
Protected Sub OnSorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
Me.BindGrid(e.SortExpression, ddlCity.SelectedItem.Value, ddlCountry.SelectedItem.Value)
End Sub
Screenshot
