We help IT Professionals succeed at work.
Get Started

Transfer selected rows from one gridview to the other

Last Modified: 2013-12-16

I have two gridviews, side by side. and I have checkboxes for all rows.  I have a text box where user can input some comments. also a button to send the selected rows from one grid (left grid) to the other (grid on right).
When I click on the button, I want transfer the selected row(s) of left grid along with comments in the textbox appended to the row(s) (in last column) to the grid on right. I want the newly added rows to be appended at the top of the right grid and the rows must be sorted and should appear in different color.

I'm using two datasets and binding them to gridview. In button click event, I'm finding which rows have been checked in the gridview and I'm copying the corresponding row in dataset1 based on index of the gridview, attaching the remarks column and then attaching the row to dataset2. ( I don't think this  is a good approach...because index of a particular row may not be same in Gridview1 and dataset1). Now when I'm adding the rows in dataset2, they appear at the bottom and that too not in order.finally I should be able to identify the new rows in dataset2 to update in the database.

1.Is there any way that I can get the selected rows of grid view and identify the same row in dataset?
2. When we add the row(s) to second dataset, how can I get them at the top and sorted and displayed with different font color.
3. how can I identify new rows in a dataset (ofcourse I can have a hidden flag column which I can flag as true if it came from different dataset, just wondering for better solution)

The following is the code for transferring rows from one grid to the other. This is as far as I could get.

Some code snippet would be highly helpful.

Thanks in advance.

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
   Dim dtPermitted As DataTable = New DataTable
        dtPermitted = Session("PermittedItems")
        Dim dtNonPermitted As DataTable = New DataTable
        dtNonPermitted = Session("NonPermittedItems")

        Dim i As Integer
        For i = Me.GridView1.Rows.Count - 1 To 0 Step -1

            If (CType(Me.GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox).Checked) Then
                Dim r As DataRow = dtPermitted.NewRow
                r(0) = dtNonPermitted.Rows(i)(0)
                r(1) = dtNonPermitted.Rows(i)(1)
                r(2) = 1 ' flag
            End If

Watch Question
Development Manager
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE