add datarows to a listbox with value !

vincentnl

Active member
Joined
Mar 29, 2003
Messages
40
Location
Netherlands
I create a datarow array and want to add two fields of that array to my listbox. One column of courseNames (displaymember) and one courseID (valuemember)

Dim draCourses As DataRow()
draCourses = drSelectedClub.GetChildRows("ClubsCourses")

lstCourses.datasource = draCourses
lstCourses.DisplayMember = "CourseName"
lstCourses.ValueMember = "CourseID"

But now entire rows are returned when i ask for the valuemember of a list selection !

The other method i thought of was addint the items one by one, but it does not seem possible to add a value to an item (it will display one, and return the other)

Load listbox for courses
Dim drCourse As DataRow
For Each drCourse In draCourses
lstCourses.Items.Add(drCourse.Item("CourseName"))
Next

Can anybody help me out?
 
You can add custom classes to a listbox that can have as many
properties as you want. The return value of the ToString method will
be what is shown. For example,
Code:
Public Class MyListItem
  Private _dataValue As Object, _text As String

  Public Sub New(text As String)
    _text = text
  End Sub

  Public Property Text As String
    Get
      Return _text
    End Get
    Set(ByVal value As String)
      _text = value
    End Set
  End Property

  Public Property DataValue As Object
    Get
      Return _dataValue
    End Get
    Set(ByVal value As Object)
      _dataValue = value
    End Set
  End Property

  Public Function ToString() As String
    Return _text
  End Function
End Class
At this point, you can add one of these to a listbox and the _text
property will be displayed.
Code:
Dim item As New MyListItem("Life, The Universe, and Everything")
item.DataValue = 42
lstListbox.Items.Add(item)

MessageBox.Show(lstListbox.Items(0).DataValue)) shows 42
 
Thanks for your reply, It is a nice work around. Unfortunately I do not like the beauty of it.

I can bind a dataset to a listbox, i can bind an array to a listbox, but i cannot bind an array of datarows to the listbox.

When I get home, perhaps I will try to create a table in stead of a datarow array. Don;t know wether that exists. I am exploring ADO and .net. And see if i can bind that to a list box both displaymember and valuemember.

vincent
 
Back
Top