Click here to Skip to main content
15,064,072 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
I'm looking for some help with populating 2 Listboxes on my Form. Listbox1 is bound to a Dataview and shows SQL selected data from a Stored Procedure:

VB
Dim Descriptions As DataView = New DataView(DS.Tables("Descriptions"))
ListBox1.DataSource = Descriptions
ListBox1.DisplayMember = "Product_Description"
ListBox1.ValueMember = "Product_Desc_ID"
ListBox1.SelectedIndex = -1


When i try the following code, it doesn't display the value from Listbox1 in Listbox2, it only displays "System.Data.DataRowView"

VB
'move one item from L1 to L2 ->
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       If ListBox1.SelectedIndex < 0 Then
           MsgBox("Please select an item to move from the Listbox on the left.", vbCritical, "ListBox1 Validation Error")
       Else
           If ListBox1.Items.Count > 0 Then
               ListBox2.Items.Add(ListBox1.SelectedItem)
           End If
       End If
   End Sub


Also, i need to be able to move the selected value - Product_Desc_ID into the second Listbox2 aswell as displaying the the Text from Listbox1 in Listbox2.

Thanks
Posted
Updated 24-Mar-10 9:14am
v2

Orbie wrote:
ListBox1.DisplayMember = "Product_Description"
ListBox1.ValueMember = "Product_Desc_ID"



Orbie wrote:
it only displays "System.Data.DataRowView"


Because yo didn't set the Display and Value members. The object is moved, but it's shown according to the rules set in the new listbox.


Orbie wrote:
Also, i need to be able to move the selected value - Product_Desc_ID into the second Listbox2 aswell as displaying the the Text from Listbox1 in Listbox2.


You are - you're moving the object that contains that data, you're just not telling it how to render it.
   
Hi Christian,
Thanks for your response but i'm a bit confused! Do i need to set both the ListBox2.ValueMember and Listbox2.DisplayMember according to the values from Listbox1?

Your help is appreciated!
   
Assuming that you're second listbox is not data bound, you don't want to add the ListBox1.SelectedItem by itself. Try ListBox1.SelectedValue if you want the ValueMember associated with the SelectedItem. You can just use ListBox1.Text if you want the DisplayMember.
   
v2
Please don't push 'answer' to ask more questions.

Yes, of course. If you don't tell ListBox2 how you want it to render your data, how will it know ?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900