Link to home
Start Free TrialLog in
Avatar of skolakanuru
skolakanuru

asked on

Transfer selected rows from one gridview to the other

Hello,

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
               
                dtPermitted.Rows.Add(r)
                dtNonPermitted.Rows.RemoveAt(i)
            End If
        Next


ASKER CERTIFIED SOLUTION
Avatar of Muhammad Kashif
Muhammad Kashif
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skolakanuru
skolakanuru

ASKER

Hello,

Thank you for the comments.

1)How can I get the newly added rows sorted?

Forexample:
I have A,B,C,D in dataset1 and E,F,G in dataset2. If I send A,C to the other dataset and add it, I get it as A,C, E,F,G..now if I transfer B to dataset 2, I get it as B,A,C,E,F,G. How can I get the transferred items in order i.e. A,B,C,E,F,G.

2) How can I select the exact rows in the dataset corresponding to the selected rows in gridview.
In the above code, I found rows in data set using row index of gridview. But they may not be same at all times. One such example is when I sort the rows, Indeces of a particular row are different in gridview and the corresponding dataset. How can I tackle this problem?

Thanks in advance.
I've got the solution ...just for reference for anybody having similar problem,here is the solution

I used item templates on both grids and I had a hidden column with all zeros initially. whenever I press a button to transfer rows from one grid to the other, I check for checked check box, append that row to the target dataset.

Now the problem is how to sort the dataset and have newly added rows on the top

This can be done using select statement on dataset

Dim alienrows As DataRow()

alienrows = dt.Select("REGION=1", "COUNTRYCODE ASC")

This gives an array of datarows which are transferred and use another select to get the native rows

 Dim selectstring As String = "REGION=0"
        nativerows = dt.Select(selectstring)

now merge these two arryas to get the dataset with newly added rows at the top and sorted.