baileyswalk
baileyswalk

Reputation: 1228

DataTextField wont work when binding to List

Public Class Item

    Public Text As String
    Public Value As Integer

End Class

Me.uxDropDown.DataSource = itms
Me.uxDropDown.DataTextField = "Text"
Me.uxDropDown.DataValueField = "Value"
Me.uxDropDown.DataBind()

Why does this not work, where itms is a List(Of Item)?

Error event triggered on the line: Me.uxDropDown.DataBind()

DataBinding: 'Project.Item' does not contain a property with the name 'Text'.

Upvotes: 1

Views: 657

Answers (1)

Hanlet Escaño
Hanlet Escaño

Reputation: 17380

the error would clearly be "DataBinding: 'Item' does not contain a property with the name 'Text'." Change those public variables as public properties instead and your error would go away.

Public Class Item
    Private _Text As String
    Private _Value As Integer

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

    Public Property Value() As Integer
        Get
            Return _Value
        End Get
        Set(ByVal value As Integer)
            _Value = value
        End Set
    End Property
End Class

Upvotes: 2

Related Questions