Solved

How to add the records ticked from gridview1 to gridview2

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

17 Experts available now in Live!

Get 1:1 Help Now