Listbox
How can I retrieve the values the user selected from a listbox with a selection mode set to = SelectionMode.MultiExtended?
How can I retrieve the values the user selected from a listbox with a selection mode set to = SelectionMode.MultiExtended?
Hi,
Use the follwoing code to select the items
Dim obj As Object
For Each obj In ListBox1.SelectedItemsMsgBox(obj.ToString)
Nextwith regards,
Sadha Sivam S
But this doesn't give me the value, but a messagebox with "System.Data.DataRowView".
From what you appear to have done - you need to specifically cast each item to a datarowview which is probably what was used in the databinding scenario.
The following code show demonstate this - most of the form load is creating a datatable and then a dataview from it which is being used for the databinding on the listbox.
The button click event will display the selected item values.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Rows.Add(1, 2)
dt.Rows.Add(3, 4)
dt.Rows.Add(5, 6)
'//Create a Listbox bound to a dataview type
Dim dv As DataView = dt.DefaultView
ListBox1.DataSource = dv
ListBox1.DisplayMember = "col1"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As DataRowView
For Each obj In ListBox1.SelectedItems
MsgBox(obj(0).ToString) '//Will display Value
Next
End Sub
End Class