moving data between two DT bound listboxes

Rossy

Member
Joined
Jun 16, 2005
Messages
9
Programming Experience
1-3
I have two listboxes(source and destination). Both are bound to their own data tables. Im trying to set this up so that i can select multiple fields from the source and then click a button that then pushes them to the destination data table and removes them from the source data table.

This sounds easy to me but i cant seem to get it to function. Moving the fields over to the destination so far is not a problem but i can't seem to remove the fields from the source DT correctly. Can someone give me some insight?
 
The Datatable.Rows property has all the members you need to remove/add items to and from a datatable's row collection. If you can add the items to the destintation table then you must have a reference to the datarow you are moving so simply remove it fromt the source table. Or if you wish to do a database update later instead of remove use .Delete
 
Well, the first problem is that I am forced to add all the rows before removing any because as soon as i remove a row the listbox gets refreshed and the SelectedItems are lost. So here is the snippet of code i have to add a row to the chosen table.


VB.NET:
For index = 0 To max_index
 
dRowView = lstAvailable.SelectedItems.Item(index)
 
dRow = dRowView.Row
 
dtChosen.Rows.Add(dRow.ItemArray())
 
Next
The add works all fine and dandy. Now to delete i try the following:

VB.NET:
For index = 0 To max_index
 
dRowView = lstChosen.Items.Item(max_index)
 
dRow = dRowView.Row
 
dtAvailable.Rows.Remove(dRow)
 
max_index = max_index - 1
 
Next
However this breaks at the row removal and says that the given row does not exist in the specified dataset. Any ideas how to fix this?
 
Last edited by a moderator:
VB.NET:
For index = 0 To max_index

dRowView = lstChosen.Items.Item(max_index)

dRow = dRowView.Row

If dtAvailable.Rows.Contains(drow) Then

dtAvailable.Rows.Remove(dRow)
 
End If

max_index = max_index - 1

Next

That will stop the error but it looks to be that there is a problem with the max_index variable. Set a breakpoint and check to see if that variable is the correct index for the row you want to remove.
 
When i step through them, the dRow item array lists the row that i want to take out, the only thing is now it doesn't error due to the if statement.

If you look at my original code to add a row

VB.NET:
        For index = 0 To max_index

            dRowView = lstAvailable.SelectedItems.Item(index)

            dRow = dRowView.Row

           [SIZE=3]dtChosen.Rows.Add(dRow.ItemArray())[/SIZE]

        Next
I originally tried this:


VB.NET:
        For index = 0 To max_index

            dRowView = lstAvailable.SelectedItems.Item(index)

            dRow = dRowView.Row

           [SIZE=3] dtChosen.Rows.Add(dRow)[/SIZE]

        Next
But it gave me an error saying this row already belongs to another table(dtAvailable)? I don't see why this would matter or not. Im pretty new to vb and might be way off but if the code to remove is remove(drow) then i would think that to add would be add(drow) and not add(drow.itemarray()). Maybe this is part of the problem?
 
Back
Top