ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Count and retrieve checked CheckBox column value in ASP.Net GridView using C# and VB.Net<p>Hey <a class="username" rel="Alauddin">Alauddin</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; DataKeyNames=&#34;HobbyId&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chkSelect&#34; runat=&#34;server&#34; Checked=&#39;&lt;%# Eval(&#34;IsSelected&#34;) %&gt;&#39; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;Hobby&#34; HeaderText=&#34;Hobby&#34; ItemStyle-Width=&#34;150px&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnSave&#34; runat=&#34;server&#34; Text=&#34;Save&#34; OnClick=&#34;Save&#34; /&gt; &lt;asp:Label ID=&#34;lblMessage&#34; runat=&#34;server&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Configuration; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.BindGrid(); } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies&#34;)) { 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(&#34;chkSelect&#34;) as CheckBox).Checked; if (isSelected) { hobbyName += row.Cells[1].Text + &#34;,&#34;; count++; lblMessage.Text = &#34;Hobbies are : &#34; + hobbyName.TrimEnd(&#39;,&#39;) + &#34;&lt;br/&gt;Count = &#34; + count; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies&#34;) 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(&#34;chkSelect&#34;), CheckBox)).Checked If isSelected Then hobbyName += row.Cells(1).Text &amp; &#34;,&#34; count += 1 lblMessage.Text = &#34;Hobbies are : &#34; &amp; hobbyName.TrimEnd(&#34;,&#34;c) &amp; &#34;&lt;br/&gt;Count = &#34; &amp; count End If Next End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/Dlzl2xn.gif" alt="" width="327" height="347" /></p>https://www.aspforums.net:443/Threads/590782/Count-and-retrieve-checked-CheckBox-column-value-in-ASPNet-GridView-using-C-and-VBNet/https://www.aspforums.net:443/Threads/590782/Count-and-retrieve-checked-CheckBox-column-value-in-ASPNet-GridView-using-C-and-VBNet/Thu, 20 Dec 2018 00:24:09 GMT