Hello,
Here i have used GridView with Two Checkbox list one for Country and Other for ContactTitle and i am fetching data from Customers Table (Northwind Database).
firstly data will be get in this manner
SELECT CustomerID,CompanyName,ContactTitle,Address,Country,ContactTitle FROM Customers
then if i check the chkType first time then there will not be any changes becouse its in the And Condition and i have written code like that if we check country and then check the chkType then gridview will be changed
HTML:
<form id="form1" runat="server">
<div>
Check Countries
<asp:CheckBoxList ID="chkCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Counrty_Selected">
<asp:ListItem Text="Germany" />
<asp:ListItem Text="Spain" />
<asp:ListItem Text="USA" />
</asp:CheckBoxList>
<br />
Check Type
<asp:CheckBoxList ID="chlType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Type_Selected">
<asp:ListItem Text="Owner" />
<asp:ListItem Text="Sales representative" />
<asp:ListItem Text="Order Administrator" />
</asp:CheckBoxList>
<br />
GridView:
<asp:GridView ID="gvDemo" AutoGenerateColumns="false" PageSize="10" AllowPaging="true"
OnPageIndexChanging="gvDemo_PageIndexChanging" runat="server" Width="500">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
</Columns>
</asp:GridView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindList();
}
}
protected void gvDemo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDemo.PageIndex = e.NewPageIndex;
BindList();
}
private void BindList()
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string query = "SELECT CustomerID,CompanyName,ContactTitle,Address,Country,ContactTitle FROM Customers";
string condition = string.Empty;
foreach (ListItem item in chkCountries.Items)
{
condition += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(condition))
{
condition = string.Format(" where Country in ({0})", condition.Substring(0, condition.Length - 1));
}
string condition2 = string.Empty;
foreach (ListItem item in chlType.Items)
{
condition2 += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(condition2))
{
condition2 = string.Format(" AND ContactTitle in ({0})", condition2.Substring(0, condition2.Length - 1));
}
SqlCommand cmd = new SqlCommand(query + condition + condition2); using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con; sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
gvDemo.DataSource = ds;
gvDemo.DataBind();
}
}
}
}
protected void Counrty_Selected(object sender, EventArgs e)
{
this.BindList();
}
protected void Type_Selected(object sender, EventArgs e)
{
foreach (ListItem item in chkCountries.Items)
{
if (item.Selected == true)
{
this.BindList();
}
}
}
}