Selected value of a listbox

rowternet
rowternet used Ask the Experts™
on
Hi,

(Winform Application, VS2008, VB.NET)

I have 2 listboxes on my form.(lstb_Drives and lstb_SelDrives)
lstb_Drives is populated from the database. It has the valuemember(Drive_ID) and displaymember(Drive Name).(Populated using a dataset)
If i select a value in the lstb_Drives  list box i am moving it to the lstb_SelDrives list box.
This part is going good.
I need to access the value member of each item in the lstb_SelDrives listbox. How can i get the value members?

Thanks
Private Sub SelectOneValue()
        Dim ht As New Hashtable
        If (lstb_Drives.SelectedIndex <> -1) Then
            MessageBox.Show(lstb_Drives.SelectedValue.ToString)
            ht.Add(lstb_Drives.SelectedValue, lstb_Drives.Text) 'lstb_Drives.SelectedValue has Drive_ID , lstb_Drives.Text has Drive Name'
            lstb_SelDrives.Items.Add(ht.Item(lstb_Drives.SelectedValue))
        End If
End Sub

'Method to retrieve the listbox values(Drive ID Values have to be retrieved from this)
Private Sub selDrivecount()
        Dim myorder As String = " and (S.Drive_id = '"
	        For selRtCount = 0 To lstb_SelDrives.Items.Count - 1
            If selRtCount = 0 Then
                lstb_SelDrives.SetSelected(selRtCount, True)
                'myorder = myorder & lstb_SelDrives.SelectedValue.ToString & "'" '''' Tried both ways but did not work
                myorder = myorder & lstb_SelDrives.SelectedItem.ToString & "'"   ' I Can retrieve the display member using this.
            Else
                lstb_SelDrives.SetSelected(selRtCount, True)
                'myorder = myorder & " or S.Drive_id='" & lstb_SelDrives.SelectedItem.ToString & "'"
                myorder = myorder & " or S.Drive_id='" & lstb_SelDrives.SelectedValue.ToString & "'"
            End If
        Next
        myorder = myorder & ")"
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
Try this:

                myorder = myorder & CType(lstb_SelDrives.SelectedItem, DataRowView).Item("Drive_ID") & "'"
Hi  Zhaolai,

Thanks for your reply.
I did not check if that works but i have it working in a different way.
I implemented  the feature that i wanted in a different way.
Awarding points to you.

Thanks
Top Expert 2010

Commented:
Thanks. Glad you found your way out.
Going forward, you can award points and close the question by yourself.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial