views:

377

answers:

1

Solution

This is what I came up with:

Public Class IndexedDropDownItem
    Private _KeyCode, _Display As String
    Public Property KeyCode() As String
        Get
            Return _KeyCode
        End Get
        Set(ByVal value As String)
            _KeyCode = value
        End Set
    End Property
    Public Property Display() As String
        Get
            Return _Display
        End Get
        Set(ByVal value As String)
            _Display = value
        End Set
    End Property
    Sub New(ByVal KeyIndex As String, ByVal ItemDisplay As String)
        KeyCode = KeyIndex
        Display = ItemDisplay
    End Sub
    Public Overrides Function ToString() As String
        Return String.Format("{0} - {1}", KeyCode, Display)
    End Function
End Class

Implementation:

With myDropDown
    Dim oItem As IndexedDropDownItem = Nothing
    For Each dr As Data.DataRow In oTemp.Rows
        oItem = New IndexedDropDownItem(dr.Item("key_code"), _ 
                                        dr.Item("descript"))
        .Items.Add(oItem)
        oItem = Nothing
    Next
End With

Manipulation:

Dim _KeyCode, _Display As String
With CType(dataPathComboBox.SelectedItem, IndexedDropDownItem)
    _KeyCode = .KeyCode
    _Display = .Display
End With

I hope this will help someone out!


I have a ComboBox that is populated from a DataTable:

With myComboBox
    .DataSource = myDataTable
    .DisplayMember = "descript"
    .ValueMember = "key_code"
End With

I want to be able to have the DisplayMember show "key_code - descript", while retaining the value I have set.

Is this even possible? Thanks

+2  A: 

Since you are using a datatable you would need to create a new column that computes the value.

I personally have moved to using objects to do my databinding and with it in my class I simply add another public property "ListDisplayText" that does the formatting for me.

Mitchel Sellers
Could you point me towards an example or sample? I think I grasp what you are saying but I want to be sure.
Anders
Looks like you got yourself to a solution! That is along the lines of what i was taking about.
Mitchel Sellers
+ points for you sir, thanks for the push in the right direction :)
Anders