Count and retrieve checked CheckBox column value in ASP.Net GridView using C# and VB.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

Hi dear,

The below link has a very good example to populate checkbox.

Populate (bind) CheckBox in GridView from database in ASP.Net using C# and VB.Net

From this example I don't need update part.

I need to pass the checked value in a string with comma separation and count how many box is chekced. Example is given below:

When checked 1 box result will be:

Swimming

Count=1

When checked 3 box result will be:

Swimming,Dancing,Singing

Count = 3 

Thanks in advance.

You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago

Hey Alauddin,

Please refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="HobbyId">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" Checked='<%# Eval("IsSelected") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Hobby" HeaderText="Hobby" ItemStyle-Width="150px" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save" />
<asp:Label ID="lblMessage" runat="server" />

Namespaces

C#

using System.Data;
using System.Configuration;
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)
    {
        this.BindGrid();
    }
}

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

protected void Save(object sender, EventArgs e)
{
    string hobbyName = string.Empty;
    int count = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;
        if (isSelected)
        {
            hobbyName += row.Cells[1].Text + ",";
            count++;
            lblMessage.Text = "Hobbies are : " + hobbyName.TrimEnd(',') + "<br/>Count =  " + count;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.BindGrid()
    End If
End Sub

Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies")
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim hobbyName As String = String.Empty
    Dim count As Integer = 0

    For Each row As GridViewRow In GridView1.Rows
        Dim isSelected As Boolean = (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked

        If isSelected Then
            hobbyName += row.Cells(1).Text & ","
            count += 1
            lblMessage.Text = "Hobbies are : " & hobbyName.TrimEnd(","c) & "<br/>Count =  " & count
        End If
    Next
End Sub

Screenshot