I'm trying to add an item into a combobox after it's bind with the database so the combobox can have something like this:
Combobox
< Select Option >
Value 1
Value 2
Value 3
Tried with the code below but i'm getting the error "Items collection cannot be modified when the DataSource property is set." It's doesn't allow me to add another item after the combo box is bind. How can add an extra item into the combo box with a binded combo box?
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