CheckBoxList within GridView Cell Count all Selected Items

Checkboxlist within GridView cell count all selected items.

Does the Checkboxlist within gridview cell counts all selected items?

Let us see.

Create Table Here I Provide Script

CREATE TABLE [dbo].[Food](

     [Fid] [bigint] primary key IDENTITY(1,1) NOT NULL,

     [Fname] [nvarchar](100) NULL,

     [Fprice] [bigint] NULL,

     [Recstatus] [char](1) NULL,



INSERT INTO Food(Fname,Fprice,Recstatus)VALUES('Dal',2000,'A')

INSERT INTO Food(Fname,Fprice,Recstatus)VALUES('Rice',52000,'A')

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FindingCheckboxlistSelectedItems.aspx.cs"

    Inherits="FindingCheckboxlistSelectedItems" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head id="Head1" runat="server">




    <form id="form1" runat="server">


        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

            GridLines="Horizontal" BackColor="White" BorderColor="#336666" BorderStyle="Double"



                <asp:TemplateField HeaderText="Product">


                        <asp:TextBox ID="TextBox1" BackColor="BurlyWood" runat="server"></asp:TextBox>



                <asp:TemplateField HeaderText="Product">


                        <asp:CheckBoxList ID="CheckBoxList1" RepeatLayout="Flow" RepeatDirection="Horizontal"


                            <asp:ListItem Text="One" Value="1">


                            <asp:ListItem Text="Two" Value="2">


                            <asp:ListItem Text="Three" Value="3">


                            <asp:ListItem Text="Four" Value="4">



                        <br />




            <FooterStyle BackColor="White" ForeColor="#333333" />

            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />

            <RowStyle BackColor="White" ForeColor="#333333" />

            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />

            <SortedAscendingCellStyle BackColor="#F7F7F7" />

            <SortedAscendingHeaderStyle BackColor="#487575" />

            <SortedDescendingCellStyle BackColor="#E5E5E5" />

            <SortedDescendingHeaderStyle BackColor="#275353" />



    <asp:Button ID="Button1" runat="server" Text="Get Data" OnClick="Button1_Click" />




To  Bind Gridview  Using This Method

public void bindgrid()

    SqlCommand cmd = new SqlCommand("select * from food ", con);

    cmd.CommandType = CommandType.Text;

    SqlDataAdapter da = new SqlDataAdapter();

    da.SelectCommand = cmd;

    DataTable dt = new DataTable();



    if (dt.Rows.Count > 0)


        GridView1.DataSource = dt;





        DataRow dr = dt.NewRow();


        GridView1.DataSource = dt;





protected void Page_Load(object sender, EventArgs e)


    if (!IsPostBack)






protected void Button1_Click(object sender, EventArgs e)

    foreach (GridViewRow GVR in GridView1.Rows)


       CheckBoxList chk = (CheckBoxList)GVR.Cells[1].FindControl("CheckBoxList1");

       string store = "";

       foreach (ListItem li in chk.Items)


            if (li.Selected)


                store += li.Value + ";";



        TextBox TextBox1 = (TextBox)GVR.Cells[0].FindControl("TextBox1");

        TextBox1.Text = store;