Net-informations.com
SiteMap  | About    

Checkbox in ASP.NET GridView Control

The main advantages of the GridView control over other data-bound controls is its ability to automatically take advantage of data source capabilities. Also the GridView Control provides programmers ability to use any type of Web control in its columns using TemplateField.

Inserting a checkbox in gridview header template and check or uncheck all rows is a common requirement in most of the asp.net programs. In this lesson you will know how to use the CheckBox inside Gridivew control and how to select the multiple checkboxes and to get the selected CheckBoxes value will be displayed. Using this method you can Delete Multiple Rows In GridView In Asp.Net Using Checkbox.

Selecting multiple checkboxes inside a GridView control

checkbox in gridview

Database

In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.

Download

Using Checkbox in ASP.NET GridView Control

You can insert a CheckBox control inside a GridView by using TemplateField.

checkbox select in gridview

How to fire an event in a checkbox inside a GridView

Here we placed a Button control for fire and event to retrieve the selected rows values from GridView to a Label on a Button Click event. From the following program you can learn how to fire an event and retrieve the selected values from GridView Control.

Default.aspx

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView Checkbox</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#BFE4FF" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkCtrl" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="stor_id" HeaderText="Stor ID" ItemStyle-Width="70" />
            <asp:BoundField DataField="stor_name" HeaderText="Stor Name" ItemStyle-Width="150" />
            <asp:BoundField DataField="state" HeaderText="State" ItemStyle-Width="50" />
        </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="btnDisplay" runat="server" Text="Show selected Data" OnClick="btmDisplay_Click" />
    <br /><br />
    <asp:Label ID="lblmsg" runat="server" />
    </form>
</body>
</html>

C# Source Code

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            string sql = null;
            string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****";
            sql = "select stor_id,stor_name,state from stores";
            SqlConnection connection = new SqlConnection(connetionString);
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);
            adapter.SelectCommand = command;
            adapter.Fill(ds);
            adapter.Dispose();
            command.Dispose();
            connection.Close();
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
    }
    protected void btmDisplay_Click(object sender, EventArgs e)
    {
        string data = "";
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
                if (chkRow.Checked)
                {
                    string storid = row.Cells[1].Text;
                    string storname = row.Cells[2].Text;
                    string state = row.Cells[3].Text;
                    data = data + storid + " ,  " + storname + " , "  + state + "<br>";
                }
            }
        }
        lblmsg.Text = data;
    }
}

VB.Net Source Code

  

Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim adapter As New SqlDataAdapter()
            Dim ds As New DataSet()
            Dim sql As String = Nothing
            Dim connetionString As String = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****"
            sql = "select stor_id,stor_name,state from stores"
            Dim connection As New SqlConnection(connetionString)
            connection.Open()
            Dim command As New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds)
            adapter.Dispose()
            command.Dispose()
            connection.Close()
            GridView1.DataSource = ds.Tables(0)
            GridView1.DataBind()
        End If
    End Sub
    Protected Sub btmDisplay_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim data As String = ""
        For Each row As GridViewRow In GridView1.Rows
            If row.RowType = DataControlRowType.DataRow Then
                Dim chkRow As CheckBox = TryCast(row.Cells(0).FindControl("chkCtrl"), CheckBox)
                If chkRow.Checked Then
                    Dim storid As String = row.Cells(1).Text
                    Dim storname As String = row.Cells(2).Text
                    Dim state As String = row.Cells(3).Text
                    data = (Convert.ToString((Convert.ToString((data & storid) + " ,  ") & storname) + " , ") & state) + "<br>"
                End If
            End If
        Next
        lblmsg.Text = data
    End Sub
End Class






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.