Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How To Add A CheckBox Column to GridView in ASP.NET

0.00/5 (No votes)
18 Oct 2014 1  
How to add a checkbox column to GridView to ASP.NET

Many a times, we need to give users a Checkbox in a GridView that users can use to select multiple rows from the GridView. In previous posts, we saw how we can do this in Windows Forms. In this post, we will see how we can add a CheckBox column to an existing GridView control in ASP.NET.

Let’s get started. We assume that we have a GridView with an existing set of columns getting populated with some data. So, we have the below definition of GridView in our ASPX page.

<asp:gridview ID="grdData" runat="server" 
AutoGenerateColumns="False" CellPadding="4" CssClass="grdData"
            ForeColor="#333333" GridLines="None" Width="200">
            <alternatingrowstyle BackColor="White" 
            ForeColor="#284775"></alternatingrowstyle>
            <columns>
                <asp:boundfield DataField="ID" 
                HeaderText="ID"></asp:boundfield>
                <asp:boundfield DataField="Name" 
                HeaderText="Name"></asp:boundfield>
            </columns>
            <editrowstyle BackColor="#999999"></editrowstyle>
            <footerstyle BackColor="#5D7B9D" 
            Font-Bold="True" ForeColor="White"></footerstyle>
            <headerstyle BackColor="#5D7B9D" 
            Font-Bold="True" ForeColor="White"></headerstyle>
            <pagerstyle BackColor="#284775" 
            ForeColor="White" HorizontalAlign="Center"></pagerstyle>
            <rowstyle BackColor="#F7F6F3" 
            ForeColor="#333333"></rowstyle>
            <selectedrowstyle BackColor="#E2DED6" 
            Font-Bold="True" ForeColor="#333333"></selectedrowstyle>
            <sortedascendingcellstyle BackColor="#E9E7E2"></sortedascendingcellstyle>
            <sortedascendingheaderstyle BackColor="#506C8C"></sortedascendingheaderstyle>
            <sorteddescendingcellstyle BackColor="#FFFDF8"></sorteddescendingcellstyle>
            <sorteddescendingheaderstyle BackColor="#6F8DAE"></sorteddescendingheaderstyle>
        </asp:gridview>

And we have the below code to populate the GridView with some dummy data.

private void LoadGridData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    for (int i = 0; i < 10; i++)
    {
        DataRow dr = dt.NewRow();
        dr["Id"] = i + 1;
        dr["Name"] = "Student " + (i + 1);
        dt.Rows.Add(dr);
    }
    grdData.DataSource = dt;
    grdData.DataBind();
}

The output of the above code is something like below:

gridview-checkbox-1

Now, we will be adding a CheckBox column to this Grid. For doing this, we will make use of TemplateField and define a new TemplateField column in the existing set of columns as below:

<asp:templatefield HeaderText="Select">
    <itemtemplate>
        <asp:checkbox ID="cbSelect"
        CssClass="gridCB" runat="server"></asp:checkbox>
    </itemtemplate>
</asp:templatefield>

Your final GridView definition will appear like below:

<asp:gridview ID="grdData" runat="server" 
AutoGenerateColumns="False" CellPadding="4" 
CssClass="grdData" ForeColor="#333333" 
GridLines="None" Width="200">
            <alternatingrowstyle BackColor="White" 
            ForeColor="#284775"></alternatingrowstyle>
            <columns>
                <asp:templatefield HeaderText="Select">
                    <itemtemplate>
                        <asp:checkbox ID="cbSelect" 
                        CssClass="gridCB" runat="server"></asp:checkbox>
                    </itemtemplate>
                </asp:templatefield>
                <asp:boundfield DataField="ID" 
                HeaderText="ID"></asp:boundfield>
                <asp:boundfield DataField="Name" 
                HeaderText="Name"></asp:boundfield>
            </columns>
            <editrowstyle BackColor="#999999"></editrowstyle>
            <footerstyle BackColor="#5D7B9D" Font-Bold="True" 
            ForeColor="White"></footerstyle>
            <headerstyle BackColor="#5D7B9D" Font-Bold="True" 
            ForeColor="White"></headerstyle>
            <pagerstyle BackColor="#284775" ForeColor="White" 
            HorizontalAlign="Center"></pagerstyle>
            <rowstyle BackColor="#F7F6F3" ForeColor="#333333"></rowstyle>
            <selectedrowstyle BackColor="#E2DED6" Font-Bold="True" 
            ForeColor="#333333"></selectedrowstyle>
            <sortedascendingcellstyle BackColor="#E9E7E2"></sortedascendingcellstyle>
            <sortedascendingheaderstyle BackColor="#506C8C"></sortedascendingheaderstyle>
            <sorteddescendingcellstyle BackColor="#FFFDF8"></sorteddescendingcellstyle>
            <sorteddescendingheaderstyle BackColor="#6F8DAE"></sorteddescendingheaderstyle>
        </asp:gridview>

Save the code and refresh your ASPX page. Now, you will see a CheckBox appearing beside each row in your GridView.

gridview-checkbox-2

Hope you like this! Keep learning and sharing! Cheers!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here