[英]How to fill a ComboBox from SQL Server using Windows Forms in vb? Cannot bind to the new display member. Parameter name: new DisplayMember
[英]VB.NET 2005 - Cannot Bind To New Display Member - ComboBox - Array
所有,
我正在使用存储的proc使用INNER JOIN从SQL Server 2005数据库获取记录。 我正在使用VB.NET2005。我正在将参数传递给存储的proc,然后将结果加载到字符串列表中,然后使用该字符串列表填充ComboBox。
我得到的错误是:
参数异常System.ArgumentException复杂的DataBinding接受IList或IListSource作为数据源。 在System.Windows.Forms.ListControl.set_DataSource(对象值)在NotaryTrackIT.GlobalStuff.PopulateDropDownList2(ComboBox&ControlName,String TheState,布尔值自动完成)在C:\\ Users \\ admin \\ Desktop \\ NotaryTrackIT \\ NotaryTrackIT \\ NotaryTrackIT \\ Modules \\ GlobalStuff.vb :第182行。
这是方案。 我有一个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代码是(Function):
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
更改此:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.