Solved

How to add the records ticked from gridview1 to gridview2

Posted on 2012-03-12
3
265 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

624 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