Search code examples
vb.netcomboboxdataview

VB.NET - Cannot bind to the new display member


I'm trying to assign the DataSource for a ComboBox that will allow the user to select a member. I'm receiving this error when run my application:

Cannot bind to the new display member. Parameter name: newDisplayMember.

Here's my code:

Private Sub StartScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'GetAllELData()
    ddlMember.DataSource = GetMemberList()
    ddlMember.DisplayMember = "DisplayName"
    ddlMember.ValueMember = "ID"


End Sub

Private Function GetMemberList() As List(Of Member)
    Dim rval = New List(Of Member)
    Dim dv As DataView = New DataView
    Dim myConnString = ConfigurationSettings.AppSettings("ConnString")

    Try
        dv = SqlHelper.ExecuteDataset(myConnString, CommandType.StoredProcedure, "spGetData").Tables(0).DefaultView
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK)
    End Try

    For Each row As DataRowView In dv
        Dim mbrNum As String = row.Item("IMMBR_CD").ToString()
        Dim mbrName As String = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(row.Item("IMMBR_NM20").ToLower())
        Dim mbrState As String = row.Item("IMMBR_ST").ToString()

        'assigns the member data to the list of members
        rval.Add(New Member(mbrNum, mbrName, mbrState))
    Next

    Return rval
End Function

And then my class definition:

Public Class Member
    Public ID As String
    Public Name As String
    Public State As String
    Public DisplayName As String

    Public Sub New(ByVal i As String, ByVal n As String, ByVal s As String)
        ID = i
        Name = n
        State = s
        DisplayName = ID & " - " & Name & ", " & State
    End Sub

    Public Overrides Function ToString() As String
        Dim rval As String = ID & " - " & Name & ", " & State
        Return rval
    End Function

    Public Function GetID() As String
        Return ID
    End Function

    Public Function GetName() As String
        Return Name
    End Function

    Public Function GetState() As String
        Return State
    End Function
End Class

I don't know why I'm getting the error. The application correctly loads the member as intended and works just fine once I click "Continue" on the error popup. Everything I've found about the error is for people passing a table as their DataSource instead of a custom class like me and the answers contain only code snippets rather than an explanation of the why there's a problem.

Can anyone help me figure out what's wrong here?

Thanks a bunch!


Solution

  • The errors were caused by binding directly to the fields. Defining those as properties and binding to the properties solved the issue.

    Public ReadOnly Property GetID() As String
        Get
            Return Me.ID
        End Get
    End Property
    
    Public ReadOnly Property GetName() As String
        Get
            Return Me.Name
        End Get
    End Property
    
    Public ReadOnly Property GetState() As String
        Get
            Return Me.State
        End Get
    End Property
    
    Public ReadOnly Property GetDisplayName() As String
        Get
            Return Me.DisplayName
        End Get
    End Property
    

    And:

    Private Sub StartScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'GetAllELData()
        ddlMember.DataSource = GetMemberList()
        ddlMember.DisplayMember = "GetDisplayName"
        ddlMember.ValueMember = "GetID"
    
    
    End Sub