Hi developmin,
Everything will remain as it is. You just need to change the BindGrid method code as per your condition.
Check this example. Now please take its reference and correct your code.
I have created the example by refering the below article.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AllowPaging="true"
PageSize="5" OnPageIndexChanging="OnPaging" OnRowCreated="GridView1_RowCreated">
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "ALL";
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
string query = "";
if (ViewState["Filter"].ToString().ToUpper() == "ALL")
{
query = "SELECT ContactName,City,Country,PostalCode FROM Customers";
}
else if (ViewState["Filter"].ToString() == "10")
{
query = "SELECT TOP 10 ContactName,City,Country,PostalCode FROM Customers";
}
else
{
query = "SELECT ContactName,City,Country,PostalCode FROM Customers WHERE Country = @Filter";
}
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void CountryChanged(object sender, EventArgs e)
{
DropDownList ddlCountry = (DropDownList)sender;
ViewState["Filter"] = ddlCountry.SelectedValue;
this.BindGrid();
}
private void BindCountryList(DropDownList ddlCountry)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select distinct Country from customers where Country IS NOT NULL");
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataBind();
ddlCountry.Items.FindByValue(ViewState["Filter"].ToString()).Selected = true;
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Label lbl = new Label();
lbl.Text = "Country";
e.Row.Cells[2].Controls.Add(lbl);
DropDownList ddl = new DropDownList();
ddl.ID = "ddlCountry";
ddl.AutoPostBack = true;
ddl.Items.Add(new ListItem("All", "ALL"));
ddl.Items.Add(new ListItem("TOP 10", "10"));
ddl.AppendDataBoundItems = true;
ddl.SelectedIndexChanged += new EventHandler(CountryChanged);
this.BindCountryList(ddl);
e.Row.Cells[2].Controls.Add(ddl);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState("Filter") = "ALL"
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim query As String = ""
If ViewState("Filter").ToString().ToUpper() = "ALL" Then
query = "SELECT ContactName,City,Country,PostalCode FROM Customers"
ElseIf ViewState("Filter").ToString() = "10" Then
query = "SELECT TOP 10 ContactName,City,Country,PostalCode FROM Customers"
Else
query = "SELECT ContactName,City,Country,PostalCode FROM Customers WHERE Country = @Filter"
End If
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim sda As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Filter", ViewState("Filter").ToString())
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddlCountry As DropDownList = CType(sender, DropDownList)
ViewState("Filter") = ddlCountry.SelectedValue
Me.BindGrid()
End Sub
Private Sub BindCountryList(ByVal ddlCountry As DropDownList)
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim sda As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("select distinct Country from customers where Country IS NOT NULL")
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
ddlCountry.DataSource = dt
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "Country"
ddlCountry.DataBind()
ddlCountry.Items.FindByValue(ViewState("Filter").ToString()).Selected = True
End Sub
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
Dim lbl As Label = New Label()
lbl.Text = "Country"
e.Row.Cells(2).Controls.Add(lbl)
Dim ddl As DropDownList = New DropDownList()
ddl.ID = "ddlCountry"
ddl.AutoPostBack = True
ddl.Items.Add(New ListItem("All", "ALL"))
ddl.Items.Add(New ListItem("TOP 10", "10"))
ddl.AppendDataBoundItems = True
AddHandler ddl.SelectedIndexChanged, AddressOf CountryChanged
Me.BindCountryList(ddl)
e.Row.Cells(2).Controls.Add(ddl)
End If
End Sub
Screenshot
