Question How to add rows from one gridview to another after checkbox has been selected?

Joined
Oct 19, 2010
Messages
5
Programming Experience
Beginner
I have created two gridviews. GridView1 contains the data of the products while GridView2 should output the products which have been selected through a check box. I don't know how to output the rows from GridView1to GridView2 after clicking on a button (AddSelectedProducts), can you help me?

Below is the current VB.Net coding

Protected Sub AddSelectedProducts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddSelectedProducts.Click
Dim chk As CheckBox
MessageLabel.Text = String.Empty

For Each rowItem As GridViewRow In GridView1.Rows
chk = CType(rowItem.Cells(0).FindControl("ProductSelector"), CheckBox)
If chk.Checked = True Then

MessageLabel.Text &= rowItem.Cells(2).Text & " is added into your Shopping Basket<br/>"



End If
Next
End Sub

I like to say that both gridviews have been made by dragging it and putting it on the web form. SqlDatasource1 is linked to gridView1 and SqlDatasource2 is linked to gridView 2. The fields in GridView1 is the checkbox field, ProductID, ProductName, Description, Price, Category, Manufacturer and Image. In GridView2 it is the same except there is no checkbox field or Description field.
 
Hi

Not tried it, but it should be as simple as taking the row you've identified and adding it to the rows collection of gridview2. So something along the lines of :

VB.NET:
For Each rowItem As GridViewRow In GridView1.Rows
chk = CType(rowItem.Cells(0).FindControl("ProductSelector"), CheckBox)
If chk.Checked = True Then

MessageLabel.Text &= rowItem.Cells(2).Text & " is added into your Shopping Basket<br/>"

[B]GridView2.Rows.Add(rowItem)[/B]

End If
Next
End Sub

Hope that helps
 
The GridView2.Rows.Add(rowItem) is not working?

The GridView2.Rows.Add(rowItem) isn't working as visual studio says its not allowed, is there another way in vb.net to add rows from gridview1 checked rows to gridview2?
 
How to add multiple rows from a checkbox?

I am using two grid views to transfer the selected rows from one grid view to the other which is in thebasket page. Everything works but the problem is that it doesnot add the multiple rows selected, it only adds a single row which is selected. How if I select mutliple rows can I display those multiple rows?

For Each rowItem As GridViewRow In GridView1.Rows
chk = CType(rowItem.Cells(0).FindControl("ProductSelector"), CheckBox)
If chk.Checked = True Then
Session("ProductID") = rowItem.Cells(1).Text
GridView2.DataBind()
GridView2.Visible = True
End If
Next
 
Back
Top