Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to add the records ticked from gridview1 to gridview2

Posted on 2012-03-12
3
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

670 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