Solved

How to add the records ticked from gridview1 to gridview2

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now