Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to add CheckBox in a GridView in ASP.NET

Author: Alanna Kremer
Download Source Code : 1268_CheckBoxINGridView.zip

How to add CheckBox in GridView and how can you find that checkbox items in code behind file, will be explained in this article.

 

If you don’t know how to populate data in GridView using SQL Data Source, please refer to my previous article on DevASP.NET.

 

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
  4. Website > Add New Item > Web Form > Click Add
  5. Now Drag and Drop a “GridView” in Default aspx page from Data tab in Toolbox.
  6. Populate this GridView with data you want. I have populated it with “Alphabetical list of products” table of “NORTHWIND” database.
  7. The code in Default.aspx page will be like below

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
       
                ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
       
                SelectCommand="SELECT [ProductID], [CategoryID], [UnitPrice], [UnitsInStock], [UnitsOnOrder] FROM [Alphabetical list of products]">
       
            </asp:SqlDataSource>
       
     
       
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
       
                AutoGenerateColumns="False" DataKeyNames="ProductID"
       
                DataSourceID="SqlDataSource1">
       
                <Columns>
       
                <asp:TemplateField>
       
                <ItemTemplate>
       
                    <asp:CheckBox ID="CheckBox1" runat="server" />
       
                </ItemTemplate>
       
                </asp:TemplateField>
       
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID"
       
                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
       
                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
       
                        SortExpression="CategoryID" />
       
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
       
                        SortExpression="UnitPrice" />
       
                    <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
       
                        SortExpression="UnitsInStock" />
       
                    <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
       
                        SortExpression="UnitsOnOrder" />
       
                </Columns>
       
            </asp:GridView>
       
       
       
            <br />
       
            <asp:Button ID="Button1" runat="server" Text="Selected Products"
       
                onclick="Button1_Click" />   
       
            <br />
       
            <asp:Label ID="Label1" runat="server"></asp:Label>


    The above code is nothing difficult but I have added a <asp:TemplateField> tag in GridView. You can see in above code I have added this tag in between <Columns> tag. We have to add <ItemTemplate> tag in <asp:TemplateField> to accommodate CheckBox Control or any other control. I have also added a button and a label below Gridview.
        
  8. Now add code below in button click event in code behind file

    C#
     
    protected void Button1_Click(object sender, EventArgs e)  {
            foreach (GridViewRow gridviewrow in GridView1.Rows) {
                CheckBox checkbox = (CheckBox)gridviewrow.Cells[0].FindControl("CheckBox1");
                if (checkbox.Checked) {
                    int productID = (int)GridView1.DataKeys[gridviewrow.RowIndex].Value;
                    Label1.Text +="<br />" + productID.ToString();
                }
            }
        }

    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each gridviewrow As GridViewRow In GridView1.Rows
                Dim checkbox As CheckBox = gridviewrow.Cells(0).FindControl("CheckBox1")
                If checkbox.Checked Then
                    Dim productID As Integer = GridView1.DataKeys(gridviewrow.RowIndex).Value
                    Label1.Text += "<br />" + productID.ToString()
                End If
            Next
        End Sub

    I have used a “foreach” loop to iterate through all GridView rows. We have to get the CheckBox control to get the checked items of the GridView. If the CheckBox is checked then you can do your functionality with these checked items. I just get the ProductID and displayed it in a Label.
  9. Press F5 and see website in browser.

 I have used simple functionality to show CheckBox in GridView, you can do any functionality you want to do. 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to populate GridView using SQL Data Source in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net