Hi All,
I have a windows form that contains 2 listboxes and 2 buttons.
The listbox on the right is populated by a database routine (This is the
easy part).
The listbox on the left is populated by 1 or more selected items from the
listbox on the right after clicking an Add button.
Clicking a "Remove" button will remove the item from the left listbox and
restore it back to the right box.
I have some code that uses 3 datasets:
1 for the data from the DB for the initial populating of the right hand side
listbox called "dsRegionNamesDB" .
1 for the data that changes in the righthand listbox (lstRegionNames) called
"dsRegionNames"
1 for the data that changes in the lefthand listbox
(lstRegionNamesSelected)call "dsSelectedRegionNames"
Table(0) is used in all datasets (though probably should do this with 3
tables inside 1 dataset, but I'm still a beginner to all this VB.Net stuff
and OOP in general).
The 2 listboxes are bound to the corresponding datatables in their
respective datasets so that changes to the datatables are
immediately reflected in the listboxes.
While I can't seem to get this to work right, I do know that it is possibly
the slowest implementation of what should be very fast.
Here is the (non-working) code - (I can't get the left hand listbox to
display the actual values)
Private dsRegionNames As New DataSet
Private dsSelectedRegionNames As New DataSet
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Rebuild()
loadDBRegionNames()
End Sub
Private Sub loadDBRegionNames()
Dim dsDBRegionNames As DataSet
dsDBRegionNames = SalesRegionsDB.GetRegionNamesActive
lstRegionNames.DataSource = dsDBRegionNames.Tables(0)
lstRegionNames.DisplayMember = "reg_name"
lstRegionNames.ValueMember = "reg_idx"
dsRegionNames = dsDBRegionNames
End Sub
Private Sub Add()
Dim oRow As DataRow
If lstRegionNames.SelectedIndex >= 0 Then
oRow = dsSelectedRegionNames.Tables(0).NewRow
oRow("reg_name") = CType(lstRegionNames.SelectedItem,
DataRowView).Row("reg_name")
oRow("reg_idx") = CType(lstRegionNames.SelectedItem,
DataRowView).Row("reg_idx")
dsSelectedRegionNames.Tables(0).Rows.Add(oRow)
dsRegionNames.Tables(0).Rows.RemoveAt(lstRegionNam es.SelectedIndex)
End If
End Sub
Private Sub Remove()
Dim oRow As DataRow
If lstSelectedRegionNames.SelectedIndex >= 0 Then
oRow = dsRegionNames.Tables(0).NewRow
oRow("reg_name") = CType(lstSelectedRegionNames.SelectedItem,
DataRowView).Row("reg_name")
oRow("reg_idx") = CType(lstSelectedRegionNames.SelectedItem,
DataRowView).Row("reg_idx")
dsRegionNames.Tables(0).Rows.Add(oRow)
dsSelectedRegionNames.Tables(0).Rows.RemoveAt(lstS electedRegionNames.Selecte
dIndex)
End If
End Sub
Private Sub Rebuild()
dsRegionNames = New DataSet
dsRegionNames.Tables.Add(New DataTable)
dsRegionNames.Tables(0).Columns.Add("reg_name")
dsRegionNames.Tables(0).Columns.Add("reg_idx")
dsSelectedRegionNames.Tables.Add(New DataTable)
dsSelectedRegionNames.Tables(0).Columns.Add("reg_n ame")
dsSelectedRegionNames.Tables(0).Columns.Add("reg_i dx")
lstRegionNames.DataSource = dsRegionNames.Tables(0)
lstSelectedRegionNames.DataSource = dsSelectedRegionNames.Tables(0)
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Me.Add()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRemove.Click
Me.Remove()
End Sub
Anywho, when the Add button is pressed the value "System.Data.DataRowView"
is what appears in the left hand side instead of the actual value.
Everything else seems to be working (albeit it takes 5 seconds for the first
value to move over).
First: Why do the values not show? - it seems like it should and I've
verified the data is actually getting to the initial dataset table(0).
Second: Is there a faster way of doing this?
TIA
MC