Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I know the basics of datagridview.
I can display the contents of a table in gridview.

Now my question is that how to create a checkbox for all the records in the gridview.

Then how to select the the record based on the checkbox click.

Please do help me to get rid of this problem
Posted
Comments
[no name] 4-May-14 5:54am    
http://www.google.com/search?q=how+to+create+a+checkbox+for+all+the+records+in+the+gridview

Study this example and adapt to your need:
the aspx page:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default31.aspx.cs" Inherits="Default31" %>

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>

                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>


the code behind:
using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default31 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
            return;
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Student ID");
        dtTemp.Columns.Add("First Name");
        dtTemp.Columns.Add("Last Name");
        dtTemp.Rows.Add("1", "Peter", "Leow");
        dtTemp.Rows.Add("2", "James", "Song");
        dtTemp.Rows.Add("3", "Mick", "Sam");
        dtTemp.Rows.Add("4", "MAry", "Cool");
        GridView1.DataSource = dtTemp;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkBox = (CheckBox)row.FindControl("CheckBox1");
            if (chkBox != null && chkBox.Checked)
            {
                TextBox1.Text = row.Cells[2].Text.ToString();
            }
        }
    }
}
 
Share this answer
 
v2
Add Checkbox in Gridview

Here is the format for asp.net project and drag the gridview control


C#
<asp:gridview id="GridVwHeaderChckbox" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
                Font-Names="Verdana" PageSize="5" Width="55%" BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px">
                <alternatingrowstyle backcolor="#BFE4FF" />
                <pagerstyle bordercolor="#CCCCCC" borderstyle="Solid" borderwidth="1px" />
                <headerstyle height="30px" backcolor="#6DC2FF" font-size="15px" bordercolor="#CCCCCC">
                    BorderStyle="Solid" BorderWidth="1px" />
                <rowstyle height="20px" font-size="13px" bordercolor="#CCCCCC" borderstyle="Solid">
                    BorderWidth="1px" />
                <columns>
                    <asp:boundfield datafield="Emp_Name" headertext="Employee Name" headerstyle-width="150px" />
                    <asp:boundfield datafield="Emp_job" headertext="Job title" headerstyle-width="150px" />
                    <asp:boundfield datafield="Emp_Dep" headertext="Department" headerstyle-width="150px" />
                    <asp:templatefield itemstyle-width="40px">
                        <headertemplate>
                            <asp:checkbox id="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
                        </headertemplate>
                        <itemstyle horizontalalign="Center" verticalalign="Middle" />
                        <itemtemplate>
                            <asp:checkbox id="chkEmp" runat="server"></asp:checkbox>
                        </itemtemplate>
                    </asp:templatefield>
                </columns>
            </rowstyle></headerstyle></asp:gridview>



Select and deselect checkbox


C#
<asp:templatefield itemstyle-width="40px" xmlns:asp="#unknown">
                        <headertemplate>
                            <asp:checkbox id="chkboxSelectAll" runat="server" autopostback="true" oncheckedchanged="chkboxSelectAll_CheckedChanged" />
                        </headertemplate>
                        <itemstyle horizontalalign="Center" verticalalign="Middle" />
                        <itemtemplate>
                            <asp:checkbox id="chkEmp" runat="server"></asp:checkbox>
                        </itemtemplate>
                    </asp:templatefield>


set autopostback as true and create OnCheckedChanged event in checkbox

C#
protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)GridVwHeaderChckboxSrvr.HeaderRow.FindControl("chkboxSelectAll");
            foreach (GridViewRow row in GridVwHeaderChckboxSrvr.Rows)
            {
                CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
                if (ChkBoxHeader.Checked == true)
                {
                    ChkBoxRows.Checked = true;
                }
                else
                {
                    ChkBoxRows.Checked = false;
                }
            }
        }



Reference : http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx

Regards,
Praveen Nelge
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900