473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

populate 1 listbox with another listbox and vice versa

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


Nov 21 '05 #1
5 4351
Dave,

When I saw your problem I had to think on this solution, I wrote it first in
text however thought let me try it first. It works, however it has a side
effect. The first selection is slow. So when somebody read this, can he try
it. Because probably I can look a whole day to it and than oversee
something. After the first everything behaves at normal speed.

\\\
Private Sub Form1_Load _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim ds As New DataSet
CreateDs(ds)
Dim dc As New DataColumn("mychecker", GetType(System.Boolean))
dc.DefaultValue = True
ds.Tables(0).Columns.Add(dc)
ds.Tables(0).Columns("mychecker").DefaultValue = True
Dim dv1 As New DataView(ds.Tables(0))
Dim dv2 As New DataView(ds.Tables(0))
dv1.RowFilter = "mychecker = true"
dv2.RowFilter = "mychecker = false"
ListBox1.DataSource = dv1
ListBox1.DisplayMember = "Name"
ListBox2.DataSource = dv2
ListBox2.DisplayMember = "Name"
AddHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
AddHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
End Sub

Private Sub ListBox1_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs)
RemoveHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
RemoveHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
If ListBox1.SelectedIndices.Count > 0 Then
Dim dv As DataView = DirectCast(ListBox1.DataSource, DataView)
dv(ListBox1.SelectedIndex)("mychecker") = False
ListBox1.SelectedIndex = -1
ListBox2.SelectedIndex = -1
End If
AddHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
AddHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
End Sub

Private Sub ListBox2_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs)
RemoveHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
RemoveHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
If ListBox2.SelectedIndices.Count > 0 Then
Dim dv As DataView = DirectCast(ListBox2.DataSource, DataView)
dv(ListBox2.SelectedIndex)("mychecker") = True
ListBox1.SelectedIndex = -1
ListBox2.SelectedIndex = -1
End If
AddHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
AddHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
End Sub
///

Cor
Nov 21 '05 #2
Thanks Cor,

I'll have to study this to see if I can get it to work for me.
I think to speed these up would be to maybe use an arraylist or something
similar with less overhead than a dataset.


"Cor Ligthert" <no************@planet.nl> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
Dave,

When I saw your problem I had to think on this solution, I wrote it first in text however thought let me try it first. It works, however it has a side
effect. The first selection is slow. So when somebody read this, can he try it. Because probably I can look a whole day to it and than oversee
something. After the first everything behaves at normal speed.

\\\
Private Sub Form1_Load _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim ds As New DataSet
CreateDs(ds)
Dim dc As New DataColumn("mychecker", GetType(System.Boolean))
dc.DefaultValue = True
ds.Tables(0).Columns.Add(dc)
ds.Tables(0).Columns("mychecker").DefaultValue = True
Dim dv1 As New DataView(ds.Tables(0))
Dim dv2 As New DataView(ds.Tables(0))
dv1.RowFilter = "mychecker = true"
dv2.RowFilter = "mychecker = false"
ListBox1.DataSource = dv1
ListBox1.DisplayMember = "Name"
ListBox2.DataSource = dv2
ListBox2.DisplayMember = "Name"
AddHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
AddHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
End Sub

Private Sub ListBox1_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs)
RemoveHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
RemoveHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
If ListBox1.SelectedIndices.Count > 0 Then
Dim dv As DataView = DirectCast(ListBox1.DataSource, DataView)
dv(ListBox1.SelectedIndex)("mychecker") = False
ListBox1.SelectedIndex = -1
ListBox2.SelectedIndex = -1
End If
AddHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
AddHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
End Sub

Private Sub ListBox2_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs)
RemoveHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
RemoveHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
If ListBox2.SelectedIndices.Count > 0 Then
Dim dv As DataView = DirectCast(ListBox2.DataSource, DataView)
dv(ListBox2.SelectedIndex)("mychecker") = True
ListBox1.SelectedIndex = -1
ListBox2.SelectedIndex = -1
End If
AddHandler ListBox1.SelectedIndexChanged, _
AddressOf ListBox1_SelectedIndexChanged
AddHandler ListBox2.SelectedIndexChanged, _
AddressOf ListBox2_SelectedIndexChanged
End Sub
///

Cor

Nov 21 '05 #3
Dave,

Why are you writting "with less overhead than a dataset", where you get that
crazy idea that an arraylist has lest overhead?

Because of all things you need to make yourself when using an arraylist for
this kind of purposes you will have probably a lot of extra overhead.

Have you any authorisezed documentation about that.

Cor
Nov 21 '05 #4
I've just read that from places on the Internet. What I heard is that an
array has the least overhead of all list objects, followed by queue, then
arraylist, then ListDictionary, HashTable, HybridDictionary and SortedList
(I may have the order wrong - pulling this from my memory), then datatables,
then datasets. The arraylist is one I like because you can add and remove
ranges of values without any looping. I assume the DataSet object has to
run additional code internally to handle multiple tables which would require
more memory and time, whereas an arraylist would not need the extra
preparatory steps and space.
Whatever the case, I went ahead and re-wrote this with 1 dataset and 2
datatables (below). (I'll try this with other list objects to see which is
most efficient)

Private dsDBRegionNames As New DataSet

Private dtRegionNames As New DataTable

Private dtSelectedRegionNames As New DataTable

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Rebuild()

loadDBRegionNames()

End Sub

Private Sub loadDBRegionNames()

dsDBRegionNames = SalesRegionsDB.GetRegionNamesActive

lstRegionNames.DataSource = dsDBRegionNames.Tables(0)

lstRegionNames.DisplayMember = "reg_name"

lstRegionNames.ValueMember = "reg_idx"

dtRegionNames = dsDBRegionNames.Tables(0)

lstSelectedRegionNames.DisplayMember = "reg_name"

lstSelectedRegionNames.ValueMember = "reg_idx"

End Sub

Private Sub Add()

If lstRegionNames.SelectedIndex < 0 Then Return

Dim oRow As DataRow

oRow = dtSelectedRegionNames.NewRow

oRow("reg_name") = CType(lstRegionNames.SelectedItem,
DataRowView).Row("reg_name")

oRow("reg_idx") = CType(lstRegionNames.SelectedItem,
DataRowView).Row("reg_idx")

dtSelectedRegionNames.Rows.Add(oRow)

dtRegionNames.Rows.RemoveAt(lstRegionNames.Selecte dIndex)

End Sub

Private Sub AddAll()

Dim oItem As DataRowView

Dim oRow As DataRow

For Each oItem In lstRegionNames.Items

oRow = dtSelectedRegionNames.NewRow

oRow("reg_name") = CType(oItem, DataRowView).Row("reg_name")

oRow("reg_idx") = CType(oItem, DataRowView).Row("reg_idx")

dtSelectedRegionNames.Rows.Add(oRow)

Next

dtRegionNames.Clear()

End Sub

Private Sub Remove()

If lstSelectedRegionNames.SelectedIndex < 0 Then Return

Dim oRow As DataRow

oRow = dtRegionNames.NewRow

oRow("reg_name") = CType(lstSelectedRegionNames.SelectedItem,
DataRowView).Row("reg_name")

oRow("reg_idx") = CType(lstSelectedRegionNames.SelectedItem,
DataRowView).Row("reg_idx")

dtRegionNames.Rows.Add(oRow)

dtSelectedRegionNames.Rows.RemoveAt(lstSelectedReg ionNames.SelectedIndex)

End Sub

Private Sub RemoveAll()

Dim oItem As DataRowView

Dim oRow As DataRow

For Each oItem In lstSelectedRegionNames.Items

oRow = dtRegionNames.NewRow

oRow("reg_name") = CType(oItem, DataRowView).Row("reg_name")

oRow("reg_idx") = CType(oItem, DataRowView).Row("reg_idx")

dtRegionNames.Rows.Add(oRow)

Next

dtSelectedRegionNames.Clear()

End Sub

Private Sub Rebuild()

dtRegionNames = New DataTable

dtRegionNames.Columns.Add("reg_name")

dtRegionNames.Columns.Add("reg_idx")

dtSelectedRegionNames.Columns.Add("reg_name")

dtSelectedRegionNames.Columns.Add("reg_idx")

lstRegionNames.DataSource = dtRegionNames

lstSelectedRegionNames.DataSource = dtSelectedRegionNames

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

Private Sub btnAddAll_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddAll.Click

AddAll()

End Sub

Private Sub btnRemoveAll_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRemoveAll.Click

RemoveAll()

End Sub



"Cor Ligthert" <no************@planet.nl> wrote in message
news:ef**************@TK2MSFTNGP09.phx.gbl...
Dave,

Why are you writting "with less overhead than a dataset", where you get that crazy idea that an arraylist has lest overhead?

Because of all things you need to make yourself when using an arraylist for this kind of purposes you will have probably a lot of extra overhead.

Have you any authorisezed documentation about that.

Cor

Nov 21 '05 #5
Here's an example showing some speed differences while reading data via
DataReader, Typed/UnTyped/Wrapped Datasets and Hashtables.
The hashtable is the overall winner in performance in this scenario (but the
most difficult to manage - the tradeoff). While this is not exactly what I'm
doing, it does clearly show the effects of the overhead of DataSets
w/regards to performance.
http://www.informit.com/articles/art...31457&seqNum=7
"Cor Ligthert" <no************@planet.nl> wrote in message
news:ef**************@TK2MSFTNGP09.phx.gbl...
Dave,

Why are you writting "with less overhead than a dataset", where you get that crazy idea that an arraylist has lest overhead?

Because of all things you need to make yourself when using an arraylist for this kind of purposes you will have probably a lot of extra overhead.

Have you any authorisezed documentation about that.

Cor

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Xizor | last post by:
Let's say I run a server. I have two people using the server. Bill and Joe. Bill is at address.com/bill and Joe is at address.com/joe. Let's say Joe and Bill are both using PHP with sessions on...
3
by: reality_x | last post by:
Hello, Forgive me if I don't know the proper terminology. I have two listboxes on a form -- one listbox with values in which users can select from and one listbox blank. Upon clicking on a...
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
3
by: Chris Kettenbach | last post by:
Hello all, Quick question. I have a listbox that is populated by items from a database. When the user select something from the list, I want to populate another listbox based on the selection in...
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
2
by: padhuwork | last post by:
Hi All, Basically for ListBox and ListView controls, its possible to add checkboxes. Instead of a checkbox is it possible to add radio button? Generally, when a user clicks on a checkbox, usually...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
0
by: arnabit | last post by:
have two listbox in a page in my website. one listbox contain report owner name the other one contain the report name.. both the listbox are databound. what i wanted is that when i scroll one...
1
by: backups2007 | last post by:
Could anyone please help me. I have two dropdown list boxes. box(a) and box(b). Here's what I want to happen: When a user selects an item form box(a), box(b) then becomes disabled. And vice...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.