我正在尝试在与数据库绑定后将项目添加到组合框中,因此组合框可以具有以下内容:
组合框
< 选择选项 >
值 1
值 2
值 3
尝试使用下面的代码,但我收到错误“设置 DataSource 属性时无法修改项目集合。” 它不允许我在组合框绑定后添加另一个项目。如何使用绑定的组合框将额外的项目添加到组合框中?
Public Overloads Sub SqLoadCombo2(ByVal comboBox As ComboBox, ByVal cnnStr As String, ByVal TextField As String, ByVal ValueField As String, ByVal sAdditionalText As String, ByVal sAdditionalValue As String, ByVal sTextSeperator As String, ByVal sAdditionalTextDirection As String)
comboBox.Items.Clear()
Dim sAddText() As String
Dim sAddValue() As String
Dim iAddSize As Integer
Dim iCtr As Integer
Dim conn As New SqlConnection("SERVER=192.168.168.200,1433;DATABASE=WBIS_Laos;UID=BISSKG;PWD=BISSKG;Asynchronous Processing=false;")
Dim strSQL As String = cnnStr
Dim da As New SqlDataAdapter(strSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "Disk")
With comboBox
.DataSource = ds.Tables("Disk")
.DisplayMember = TextField
.ValueMember = ValueField
.SelectedIndex = 0
End With
'==Retrieve Additional Text and Value==
If sAdditionalText.ToString <> "" Then
sAddText = sAdditionalText.Split(sTextSeperator)
sAddValue = sAdditionalValue.Split(sTextSeperator)
iAddSize = sAddText.Length
Else
ReDim sAddText(0)
ReDim sAddValue(0)
iAddSize = 0
End If
'**Retrieve Additional Text and Value**
'==Push Additional text into combo based on the direction request==
sAdditionalTextDirection = sAdditionalTextDirection.ToString.Trim.ToUpper
If sAdditionalTextDirection <> "TOP" And sAdditionalTextDirection <> "BTM" Then
sAdditionalTextDirection = "TOP"
End If
If sAdditionalTextDirection = "BTM" Then
For iCtr = 0 To iAddSize - 1
comboBox.Items.Add(sAddText(iCtr))
comboBox.Items(comboBox.Items.Count - 1).Value = sAddValue(iCtr)
Next
Else
For iCtr = iAddSize - 1 To 0 Step -1
comboBox.Items.Insert(0, sAddText(iCtr))
comboBox.Items(0).Value = sAddValue(iCtr)
Next
End If
End Sub