VB.NET 2005 - 无法绑定到新的显示成员 - 组合框 - 数组

Posted

技术标签:

【中文标题】VB.NET 2005 - 无法绑定到新的显示成员 - 组合框 - 数组【英文标题】:VB.NET 2005 - Cannot Bind To New Display Member - ComboBox - Array 【发布时间】:2014-12-07 05:18:12 【问题描述】:

所有,

我正在使用存储过程通过 INNER JOIN 从 SQL Server 2005 数据库中获取记录。我正在使用 VB.NET 2005。我将参数传递给存储的 proc,然后将结果加载到字符串列表中,然后使用该字符串列表填充 ComboBox。

我得到的错误是:

参数异常 System.ArgumentException 复杂数据绑定 接受 IList 或 IListSource 作为数据源。在 System.Windows.Forms.ListControl.set_DataSource(对象值)在 NotaryTrackIT.GlobalStuff.PopulateDropDownList2(ComboBox&ControlName, String TheState, Boolean AutoComplete) in C:\Users\admin\Desktop\NotaryTrackIT\NotaryTrackIT\NotaryTrackIT\Modules\GlobalStuff.vb:line 182.

这就是场景。我有一个 ComboBox 正在填充来自数据库的美国州代码。这工作得很好。当光标移出此 ComboBox 时,另一个 ComboBox 应该填充该特定美国州内的县列表。但是,当光标移出ComboBox时,会产生错误,但是,美国各县的ComboBox确实有值,这是没有意义的。我不确定为什么会产生错误。

这是我的代码。

CREATE PROCEDURE GetStateCounties
(
    @TheState AS CHAR(2)
)
AS
BEGIN
    SELECT S.StateID AS SPK,
           S.StateAbbrev AS SA,
           SC.StateID AS SFK,
           SC.CountyName AS CN
    FROM States AS S
    INNER JOIN StatesCounties AS SC
    ON S.StateID = SC.StateID
    WHERE S.StateAbbrev = @TheState
    ORDER BY CN
END

填充 ComboBox 的 VB 代码是(函数):

Public Function PopulateCounties(ByVal TheState As String) As List(Of String)

    Dim LoadCounties As New List(Of String)
    Dim dr As SqlDataReader

    Using SetDatabaseConnection As SqlConnection = New SqlConnection(ConnectToDatabase)
        Using cmd As SqlCommand = New SqlCommand
            Try
                With cmd
                    .CommandText = "GetStateCounties"
                    .CommandType = CommandType.StoredProcedure
                    .Connection = SetDatabaseConnection
                    .Parameters.Add("@TheState", SqlDbType.Char).Value = TheState
                End With

                With SetDatabaseConnection
                    If .State = ConnectionState.Broken _
                    OrElse .State = ConnectionState.Closed Then
                        .Open()
                    End If
                End With

                dr = cmd.ExecuteReader

                While dr.Read
                    LoadCounties.Add(dr("CN"))
                End While
            Catch RangeEx As IndexOutOfRangeException
                MessageBox.Show(RangeEx.ToString(), _
                                "Index Out Of Range Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch CastEx As InvalidCastException
                MessageBox.Show(CastEx.ToString(), _
                                "Invalid Cast Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch ArgNullEx As ArgumentNullException
                MessageBox.Show(ArgNullEx.ToString(), _
                                "Argument Null Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch ArgEx As ArgumentException
                MessageBox.Show(ArgEx.ToString(), _
                                "Argument Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch SQLEx As SqlException
                MessageBox.Show(SQLEx.ToString(), _
                                "SQL Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch InvalidOpEx As InvalidOperationException
                MessageBox.Show(InvalidOpEx.ToString(), _
                                "Invalid Operation Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch NotSuppEx As NotSupportedException
                MessageBox.Show(NotSuppEx.ToString(), _
                                "Not Supported Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch NullRefEx As NullReferenceException
                MessageBox.Show(NullRefEx.ToString(), _
                                "Null Reference Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Finally
                With SetDatabaseConnection
                    If .State = ConnectionState.Open Then
                        .Close()
                    End If
                End With
            End Try
        End Using
    End Using

    Return LoadCounties
End Function

Private Sub ComboBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles State.LostFocus, AddressType.LostFocus, PhoneNumberType.LostFocus, EmailAddressType.LostFocus
    Try
        With CType(sender, ComboBox)
            .BackColor = Color.White
            .ForeColor = Color.Black

            If .Name.Equals("State") Then
                Dim TheCountiesToAdd = PopulateCounties(State.Text.ToString.Trim)

                For Each TheCounties As String In TheCountiesToAdd
                    With CType(Territory, ComboBox)
                        .Items.Clear()
                        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                        .AutoCompleteSource = AutoCompleteSource.ListItems
                        .DataSource = TheCountiesToAdd
                        .DisplayMember = "CN"
                        .ValueMember = "CN"
                    End With
                Next TheCounties
            End If
        End With
    Catch CastEx As InvalidCastException
        MessageBox.Show(CastEx.ToString(), _
                        "Invalid Cast Exception", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error, _
                        MessageBoxDefaultButton.Button1)
    Catch OutOfMemEx As OutOfMemoryException
        MessageBox.Show(OutOfMemEx.ToString(), _
                        "Out Of Memory Exception", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error, _
                        MessageBoxDefaultButton.Button1)
    Catch ArgEx As ArgumentException
        MessageBox.Show(ArgEx.ToString(), _
                        "Argument Exception", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error, _
                        MessageBoxDefaultButton.Button1)
    End Try
End Sub

【问题讨论】:

PopulateDropdownList2 的代码没有给出,但那是实际的异常站点。 【参考方案1】:

改变这个:

For Each TheCounties As String In TheCountiesToAdd                
    With CType(Territory, ComboBox)
        .Items.Clear()
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
        .DataSource = TheCountiesToAdd
        .DisplayMember = "CN"
        .ValueMember = "CN"
    End With
Next TheCounties

作者:

With CType(Territory, ComboBox)
     .Items.Clear()
     .AutoCompleteMode = AutoCompleteMode.SuggestAppend
     .AutoCompleteSource = AutoCompleteSource.ListItems
     .DataSource = ConvertToDataTable(TheCountiesToAdd)
     .DisplayMember = "CN"
     .ValueMember = "CN"
End With

这里的功能:

Public Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable
    Dim table As New DataTable()
    table.Columns.Add("CN")
    For Each item As T In list
        Dim row As DataRow = table.NewRow()
        row("CN") = item
        table.Rows.Add(row)
    Next
    Return table
End Function

【讨论】:

以上是关于VB.NET 2005 - 无法绑定到新的显示成员 - 组合框 - 数组的主要内容,如果未能解决你的问题,请参考以下文章

关于VB.NET2005中dataGridView的问题

空路径名不合法 (vb.net 2005 ,access 2000,)

vb.net vs2005里面的如何把DateTimePicker控件里面的显示的时间去掉,也就是我只要显示年/月/日

从 vb.NET (2003) 迁移到 vb2005 都有哪些好处?

关于VB.NET中 datagridview 刷新问题

为啥 VB.NET中DataGridView控件显示不出来绑定的数据呢?