Click here to Skip to main content
13,353,462 members (39,009 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 11 Jul 2012

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

, 11 Jul 2012
Rate this:
Please Sign up or sign in to vote.
showing the checked data from one gridview to another gridview on button click


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.


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: 

    <add name="ApplicationServices" 

       connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
             AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" 

    <add name="MyConnection" 

       connectionString="server=LocalServer;uid=sa;pwd=sa;Initial Catalog=employee"/>

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" >
                <asp:CheckBox ID="chkStatus" runat="server" />
       <asp:BoundField DataField="id" HeaderText="ID" />
       <asp:BoundField DataField ="fname" HeaderText="First Name" />
       <asp:BoundField DataField="lname" HeaderText="Last Name" />
<asp:Button ID="btnUpdate" runat="server" 

    Text="Update" onclick="btnUpdate_Click" />
<asp:GridView ID="grdViewUpDate" runat="server" 

     GridLines="None" AutoGenerateColumns="false" DataKeyNames="id" >
       <asp:BoundField DataField="id" HeaderText="ID" />
       <asp:BoundField DataField = "fname" HeaderText="First Name" />
       <asp:BoundField DataField="lname" HeaderText="Last Name" />

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;

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


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


About the Author

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

You may also be interested in...


Comments and Discussions

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