VB.NET - 无法绑定到新的显示成员

时间:2015-08-24 13:42:03

标签: vb.net combobox dataview

我正在尝试为ComboBox分配DataSource,以允许用户选择成员。我在运行我的应用程序时收到此错误:

  

无法绑定到新的显示成员。   参数名称:newDisplayMember。

这是我的代码:

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

然后我的班级定义:

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

我不知道为什么我收到错误。应用程序按预期正确加载成员,一旦我在错误弹出窗口中单击“继续”,就可以正常工作。我发现的关于错误的一切都是为了让人们将表作为他们的DataSource而不是像我这样的自定义类传递而且答案只包含代码片段,而不是解释为什么会出现问题。

有谁能帮我弄清楚这里有什么问题?

非常感谢!

1 个答案:

答案 0 :(得分:1)

错误是由直接绑定到字段引起的。将这些属性定义为属性并绑定到属性解决了这个问题。

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

@api_view(['GET'])
def my_api_view(request, format=None):
    # get data