i want bind and filter gridview data with values selected in checbox list ..
i am bind checkboxlist on pageload ....but able to filter the result the of gridview with value of checkbox list ..
when i check box nothing happen
this my code
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" DataValueField="cartid" DataTextField="sub"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" >
</asp:CheckBoxList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="prodname" HeaderText="prodname"
SortExpression="prodname" />
<asp:BoundField DataField="prodprice" HeaderText="prodprice"
SortExpression="prodprice" />
</Columns>
</asp:GridView>
</div>
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["eshopConnectionString"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(" select (sub_category + '(' + convert(nvarchar, COUNT(*)) + ')') as sub,cartid from products where category =(select distinct category from products where cartid='dr_cartid1') group by sub_category,cartid", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
CheckBoxList1.DataSource = ds.Tables[0];
CheckBoxList1.DataBind();
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(
ConfigurationManager.ConnectionStrings["eshopConnectionString"].ConnectionString.Trim());
SqlCommand cmd = new SqlCommand();
String sqlText = "select prodname,prodprice from products ";
String sqlFilterText = "";
int index = 0;
foreach (ListItem item in CheckBoxList1.Items)
{
index += 1;
if (item.Selected)
{
String paramName = "@param" + index.ToString().Trim();
SqlParameter param = new SqlParameter(paramName, SqlDbType.UniqueIdentifier);
param.Value = new Guid(item.Value.Trim());
cmd.Parameters.Add(param);
sqlFilterText += " cartid = " + paramName + " or ";
}
}
if (!String.IsNullOrEmpty(sqlFilterText))
{
sqlText += " Where " + sqlFilterText.Substring(0, sqlFilterText.Length - 3);
}
cmd.CommandText = sqlText;
cmd.Connection = cnn;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable tbl = new DataTable();
adapter.Fill(tbl);
GridView1.DataSource = tbl;
GridView1.DataBind();
}