是否可以自定义DataBound ComboBox的DisplayMember?

时间:2009-04-10 14:13:46

标签: vb.net data-binding combobox

解决方案

这就是我提出的:

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

实现:

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

操纵:

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

我希望这会帮助别人!


我有一个从DataTable填充的ComboBox:

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

我希望能够让DisplayMember显示“key_code - descript”,同时保留我设置的值。

这甚至可能吗?感谢

1 个答案:

答案 0 :(得分:3)

由于您使用的是数据表,因此需要创建一个计算值的新列。

我个人已经开始使用对象来进行数据绑定,并且在我的课程中使用它我只需添加另一个公共属性“ListDisplayText”来为我做格式化。