Sending checked value from dynamically added checkboxes of one GridView to another
showing the checked data from one gridview to another gridview on button click
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.