Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add the records ticked from gridview1 to gridview2

Posted on 2012-03-12
3
Medium Priority
?
277 Views
Last Modified: 2012-03-26
I have gridview1  - It contains data i.e 5 records and a checkbox in front of each row.
What i want is when i tick on any checkbox, the corresponding row should get appended to gridview2
0
Comment
Question by:searchsanjaysharma
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 total points
ID: 37713840
Hi,

i have an idea for your process as below.

add two grids one by one with a button control.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
	<ContentTemplate>
		<br />
		<table>
			<tr>
				<td>
				<!-- GridView With Checkbox	-->
					<asp:GridView ID="GridView1" runat="server">
						<Columns>
							<asp:TemplateField>
								<ItemTemplate>
									<asp:CheckBox ID="chkBox" runat="server" AutoPostBack="True" />
								</ItemTemplate>
							</asp:TemplateField>
						</Columns>
					</asp:GridView>
				</td>
			</tr>
			<tr>
				<td height="20px">
					<asp:Button ID="Button1" CssClass="btnDown" runat="server" Text="Append Row" />
				</td>
			</tr>
			<tr>
				<td>
					<asp:GridView ID="GridView2" runat="server">
					</asp:GridView>
				</td>
			</tr>
		</table>
	</ContentTemplate>
</asp:UpdatePanel>

Open in new window

Load the data into the GridView1 and select the checkboxes which records are needed to add to the GridView2.

click "Append Row" button to update the selected row from GridView1 to GridView2.

"Append Row" button process can be like below

1. read GridView1 row data till GridView1.Rowcount
2. check the checkbox state. if checked then get the ID (primary data or Identity value to get the data) value and append it in a string seperated by comma (selectedID).
3. If any of the checkbox is checked then get the data from based on the selectedID value (select * from table where id in (<selectedID>))
4. then bind the grid

Sample Code
protected void Button1_Click(object sender, EventArgs e)
{
	DataTable dt = new DataTable();

	if (GridView1.Rows.Count > 0) {
		string ID = "";
		bool isSelected = false;

		foreach (GridViewRow row in GridView1.Rows) {
			CheckBox chk = (CheckBox)row.FindControl("chkBox");

			if (chk.Checked == true) {
				if (isSelected) {
					ID += ",";
				}

				ID += "'" + row.Cells(1).Text.Trim + "'";
				isSelected = true;
			}
		}

		if (isSelected) {

			dt1 = GetData("SELECT * FROM Customers WHERE CustomerID IN (" + ID.ToString().Trim() + ") ORDER BY CustomerID ASC ");
			if (dt1.Rows.Count > 0) {
				
				GridView2.DataSource = null;

				GridView2.Columns.Clear();

				GridView2.AutoGenerateColumns = true;

				GridView2.DataSource = dt1;
				GridView2.DataBind();

			}

		} else {
			
			GridView2.DataSource = null;

			GridView2.Columns.Clear();

			GridView2.DataBind();

		}

	}
}

Open in new window

Hope this will help you...
0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 37756785
any luck?
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37767314
ok
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question