10 then MyCombo.Items.Add(R) Next And now I need to set th" /> 10 then MyCombo.Items.Add(R) Next And now I need to set th" /> 10 then MyCombo.Items.Add(R) Next And now I need to set th"/>

For a combobox with item = a datarow, how to point ValueMember to one of it's columns

713 Views Asked by At

I add items to a combobox like this:

For each R as DataRow in MyDataTable.Rows
  If R("ID") > 10 then MyCombo.Items.Add(R)
Next

And now I need to set the DisplayMember and ValueMember to a column of the datarow:

MyCombo.ValueMember = R("ID")
MyCombo.DisplayMember = R("Name")

I know it doesn't make sence to to use "R" as it doesn't reference to anything at this point but it's just to make an indication of what I mean ;-)

The documentation for ValueMember says: "A String representing a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object"

I know I can add the rows to a new datatable and set it to the DataSource, but as you can add any object to the combobox items, it would be nice to use the rows directly, just can't figures out how to make a reference the particular column as a string.?

1

There are 1 best solutions below

0
MrCalvin On

Maybe you cannot use a row object directly. I guess to use Valuemember you need your item objects to be wrapped in a collection which implement an ilist interface. In the old MS-Access days combobox items had natively Display- and ValueMember properties, I've always missed that in the .Net combobox control. My work-around is to use this class, which then can be used for all your ComboBoxes:

Class oComboItems
    Public items As New List(Of oDVpairs)

    Class oDVpairs
        Implements IComparable(Of oDVpairs)

        Private myDM As String
        Private myVM As Object

        Sub New(DM As String, VM As Object)
            myDM = DM
            myVM = VM
        End Sub

        Public ReadOnly Property DM() As String
            Get
                Return myDM
            End Get
        End Property

        Public ReadOnly Property VM() As Object
            Get
                Return myVM
            End Get
        End Property

        Public Function CompareTo(other As oDVpairs) As Integer Implements IComparable(Of oDVpairs).CompareTo
            Return Me.myDM.CompareTo(other.myDM)
        End Function
    End Class

    Public Sub AddItems(DisplayMember As String, ValueMemeber As Object)
        items.Add(New oDVpairs(DisplayMember, ValueMemeber))
    End Sub

    Public ReadOnly Property DisplayMember() As String
        Get
            Return "DM"
        End Get
    End Property

    Public ReadOnly Property ValueMember() As Object
        Get
            Return "VM"
        End Get
    End Property
End Class

And now add my datarows(or any other objects) to the ComboBox:

 Dim CI As New oComboItems
 For Each R As DataRow In DT_U.Rows
      If R("medlnr") > 10 Then
           CI.AddItems(R("name"), R("ID"))
      end if
 Next

CI.items.Sort()

MyCombo.DataSource = CI.Items
MyCombo.DisplayMember = CI.DisplayMember
MyCombo.ValueMember = CI.ValueMember