Check this example
<asp:CheckBoxList ID="chkProductTypes" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ProductsType_Changed">
</asp:CheckBoxList>
<br />
<asp:GridView ID = "gvProducts" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ProductId" HeaderText="Product Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "SELECT 'ALL (' + CAST((SELECT COUNT(ProductId) FROM Products) AS VARCHAR(10)) + ')' [Type], 0 [TypeId]";
query += " UNION ALL ";
query += "SELECT [Type] + ' (' + CAST((SELECT COUNT(ProductId) FROM Products WHERE ProductTypeId = TypeId) AS VARCHAR(10)) + ')' [Type], TypeId FROM ProductTypes";
chkProductTypes.DataSource = this.GetData(query);
chkProductTypes.DataTextField = "Type";
chkProductTypes.DataValueField = "TypeId";
chkProductTypes.DataBind();
chkProductTypes.Items[0].Selected = true;
ProductsType_Changed(null, EventArgs.Empty);
}
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void ProductsType_Changed(object sender, EventArgs e)
{
string query = "SELECT * FROM Products";
if (chkProductTypes.Items[0].Selected)
{
gvProducts.DataSource = this.GetData(query);
gvProducts.DataBind();
}
else
{
bool isSelected = false;
query += " WHERE ProductTypeId IN (";
foreach (ListItem item in chkProductTypes.Items)
{
if (item.Selected)
{
isSelected = true;
query += item.Value + ",";
}
}
query = query.Substring(0, query.Length - 1);
query += ")";
if (isSelected)
{
gvProducts.DataSource = this.GetData(query);
}
else
{
gvProducts.DataSource = null;
}
gvProducts.DataBind();
}
}

