ListBox .DisplayMember and ValueMember ??

  • Thread starter Thread starter perrycheung221
  • Start date Start date
P

perrycheung221

Hi all,

I got a problem of getting a display text and value from a item of a
listbox

Below is how I databind the data into myListBox

dt is a dataTable with 2 column, ID (int) and value (string)

Me.myListBox.DisplayMember = dt.Columns(1).ColumnName
Me.myListBox.ValueMember = dt.Columns(0).ColumnName
Me.myListBox.DataSource = dt

Now, I want to get the value out from myListBox:

For i As Integer = 0 To myListBox.Items.Count - 1
Dim li As ListItem
li.ItemData = lbChosenWork.Items.Item(i).Value <- I
want sth like this
li.Name = lbChosenWork.Items.Item(i).Text <- I want
sth like this
Next

However, methods .value and .text are not avaliable for me to use....

Anyone know the way to get the Display text of the item in myListBox?


Many thanks
 
Rule #1 with winforms listboxes.

You don't put value pairs in the listbox.

You put OBJECTS into the listbox.

So if you're setting the datasource to a DataTable, then the OBJECTS in the
listbox are probably DataRows (or DataTableRows), and you need to get out a
DataRow or DataTableRow ..........

( I can't remember off hand)

but its something like this.
For i As Integer = 0 To myListBox.Items.Count - 1
dim dtr as DataTableRow = nothing
dtr = lbChosenWork.Items.Item(i)

' NOW You work with the dtr



You'll have to figure out what object is in the listbox. Again, I emphasize
OBJECT, not

For i As Integer = 0 To myListBox.Items.Count - 1
dim o as object = nothing
o = lbChosenWork.Items.Item(i)

'' now look at the type of o .. or even the ToString method to
figure out what the exact object type is.
 
Back
Top