The purpose of this code is to give the illusion on the form the rows in one data grid are being moved to the other data grid. To accomplish this you will need of course a data table of some sorts to hold the data. You will also need a method that will delete the selected row from the table and another method that will insert a row into that same table.
For instance. You have a selection of baskets to place different fruit in. You want to give the user of the form functionality to be able to manipulate what fruit goes in what basket from a list of available fruit. One data grid will display the fruit currently in the selected basket. The other data grid will show the available fruit. Restricting the same fruit from being placed in the currently selected basket. The data source for the fruit in the currently selected basket is in one data table and the data source for the available fruit comes from another table.
The add and delete methods should only modify data the in the table that holds the baskets selected fruit. Each time that either add or delete method is fired it should result in the illusion of the items being removed or added to the data grids. The table with available items always has the same items always. Nothing is added or deleted from this table. It is with the code below that you will use to remove rows from the available items data table based on what is in the current data table before display.
Is this confusing enough yet?
Realize of course the following example has been abstracted and is somewhat generic. You will have to modify it to work for you and your situation. I hope it at least gives some ideas.
Private Sub BindGrids()
‘Declare and Bind two data tables.
‘I created two seperate bind methods for this.
Dim dt1 As DataTable = BindDataGrid1()
Dim dt2 As DataTable = BindDataGrid2()
‘Start looping through the first
For Each dr1 As DataRow In dt1.Rows
‘Find a matching row in data table 1
‘with a matching value in data table 2
‘and set found rows to a data rows collection
Dim dr As DataRow() = dt2.Select(dataColumnName = ” & dr1(0))
‘Starting looping through the rows found
‘and remove the rows out of the second data table.
For Each dr2 As DataRow In dr
dt2.Rows.Remove(dr2)
Next
Next
‘Instantiate a data view
Dim dv As New DataView(dt1)
‘Bind data table 1 to data grid 1
DataGrid1.DataSource = dv
DataGrid1.DataBind()
‘Re-instantiate data view
dv = New DataView(dt2)
‘Bind data table 2 to data grid 2
DataGrid2.DataSource = dv
DataGrid2.DataBind()
End Sub
Hey, if this helps, share it with a friend.
Also, use at your own risk. I take no responsibility if it blows your computer to high heavens. Don’t come hunting me down and trying to sue. You have been warned.