Click here to Skip to main content
Click here to Skip to main content

Sending checked value from dynamically added checkboxes of one GridView to another

By , 11 Jul 2012
Rate this:
Please Sign up or sign in to vote.

Introduction

This article is about getting a checkbox in each row of a GridView dynamically and a button click will take all the selected row data from one GridView to another. This is just a simple example and you can modify it however you want. This article is mainly written to help beginners use a checkboxes within a GridView.

Background

To ease user understanding, I have also included the .sql file within the project itself. Here you will find the database table I used for this project. You can just run the query and the table will be created in your SQL Server. The name of the database in my case is "employee".

The database connection is within the web.config file.

In the design view, I have two GridViews. One is "grdViewUSelData" and another is "grdViewUpdate". In "grdViewUSelData", I added a checkbox control under the columns field. There is a button "btnUpdate" to click. On page load, you will see the data from the database loaded to "grdViewUSelData" along with the checkboxes in each row to select. You can select any number of checkboxes at a time. When you click the "btnUpdate" button, it will transfer all the checked rows to the "grdViewUpdate" GridView.

Using the code

The connection string is within the web.config file under configurations, as shown below: 

<connectionStrings>
    <add name="ApplicationServices" 
       connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
             AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" 
       providerName="System.Data.SqlClient"/>
    <add name="MyConnection" 
       connectionString="server=LocalServer;uid=sa;pwd=sa;Initial Catalog=employee"/>
</connectionStrings>

In the design view, the two GridViews, checkbox under the GridView, and the button are placed under the body content and shown as below:

<asp:GridView ID="grdViewUSelData" runat="server" 
        GridLines="None" AutoGenerateColumns="false" 
        DataKeyNames = "id" EnableViewState="true" >
   <Columns>
       <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkStatus" runat="server" />
            </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField DataField="id" HeaderText="ID" />
       <asp:BoundField DataField ="fname" HeaderText="First Name" />
       <asp:BoundField DataField="lname" HeaderText="Last Name" />
   </Columns>
</asp:GridView>
<asp:Button ID="btnUpdate" runat="server" 
    Text="Update" onclick="btnUpdate_Click" />
<asp:GridView ID="grdViewUpDate" runat="server" 
     GridLines="None" AutoGenerateColumns="false" DataKeyNames="id" >
   <Columns>
       <asp:BoundField DataField="id" HeaderText="ID" />
       <asp:BoundField DataField = "fname" HeaderText="First Name" />
       <asp:BoundField DataField="lname" HeaderText="Last Name" />
   </Columns>
</asp:GridView>

And I used the following code to check whether the checkbox is checked or not in each GridView row on button click.

foreach (GridViewRow grdRow in grdViewUSelData.Rows)
{
    if (((CheckBox)grdRow.FindControl("chkStatus")).Checked)
    {  DataRow drow = dt.NewRow();
       drow["id"] = grdRow.Cells[1].Text;
       drow["fname"] = grdRow.Cells[2].Text;
       drow["lname"] = grdRow.Cells[3].Text;
       dt.Rows.Add(drow);
     }
}

This will set you up for using dynamic checkboxes in a GridView. I hope this article will help you.

License

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

About the Author

Dhyanga
Student
Nepal Nepal
Nothing is particularly hard if you divide it into small jobs. -- Henry Ford

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 11 Jul 2012
Article Copyright 2012 by Dhyanga
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid