0

我正在尝试在与数据库绑定后将项目添加到组合框中,因此组合框可以具有以下内容:

组合框

< 选择选项 >
值 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
4

3 回答 3

1

与其将组合框绑定到从头开始dataAdapter创建您自己的组合框 - 首先用您要添加到列表中的字段填充您的表格,然后循环通过您的阅读器将每个项目添加到. 在此之后,您可以将您的表格绑定到您的组合框。下面是我使用它的一个例子DataTableDataTable

Dim sqlHardware As String = "SELECT * FROM Hardware ORDER BY HardwareType"
Dim dtHardware As New DataTable, drHardware As DataRow
dtHardware.Clear()
dtHardware.Columns.Add(New DataColumn("ID", GetType(String))) '1
dtHardware.Columns.Add(New DataColumn("HardwareType", GetType(String))) ' 2
Dim commandHardware As New SqlCommand(sqlHardware, ConnMVC)
If ConnMVC.State <> 1 Then ConnMVC.Open()
Dim readerHardware As SqlDataReader = commandHardware.ExecuteReader()
If readerHardware.HasRows Then
    drHardware = dtHardware.NewRow()
    drHardware(0) = "0"
    drHardware(1) = "Select Type"
    dtHardware.Rows.Add(drHardware)
    Do While readerHardware.Read
        drHardware = dtHardware.NewRow()
        drHardware(0) = readerHardware!ID
        drHardware(1) = readerHardware!HardwareType
        dtHardware.Rows.Add(drHardware)
    Loop
    cbSoortHardware.DataSource = dtHardware
    cbSoortHardware.DisplayMember = "SoortHardware"
    cbSoortHardware.ValueMember = "ID"
    cbSoortHardware.SelectedIndex = 0
End If
于 2013-05-16T10:59:16.743 回答
1

我使用连接到源列表的 BindingSource 对象,然后将其绑定到 ComboBox。所有操作都转移到源并刷新到组合框。在带有组合框和按钮的简单示例下方:

Public Class Form1
Dim CoupledOrder As New List(Of clsCoupledOrder)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim cOrder As clsCoupledOrder
    cOrder = New clsCoupledOrder("1", "Sunday")

    CoupledOrder.Add(New clsCoupledOrder("1", "Sunday"))
    CoupledOrder.Add(New clsCoupledOrder("2", "Monday"))
    CoupledOrder.Add(New clsCoupledOrder("3", "Tuesday"))
    CoupledOrder.Add(New clsCoupledOrder("4", "Wednesday"))
    CoupledOrder.Add(New clsCoupledOrder("5", "Thursday"))
    CoupledOrder.Add(New clsCoupledOrder("6", "Friday"))
    CoupledOrder.Add(New clsCoupledOrder("7", "Saturday"))

    BindingSource1.DataSource = New BindingSource(CoupledOrder, Nothing)

    ComboBox1.DataSource = BindingSource1
    ComboBox1.DisplayMember = "Value"
    ComboBox1.ValueMember = "Description"

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim AddValue As clsCoupledOrder = New clsCoupledOrder("8", "ExtraDay")

    BindingSource1.List.Insert(0, AddValue)
End Sub

Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged

End Sub

结束类

Public Class clsCoupledOrder
Private StringDesc As String
Private GUID As String

Public Sub New(ByVal desc As String, ByVal value As String)

    Me.StringDesc = desc
    Me.GUID = value

End Sub


Public Property Description() As String
    Get
        Return StringDesc
    End Get
    Set(ByVal value As String)
        StringDesc = value

    End Set
End Property

Public Property Value() As String
    Get
        Return GUID
    End Get
    Set(ByVal value As String)
        GUID = value
    End Set
End Property

结束类

于 2018-05-29T15:13:52.767 回答
0

我的解决方案是在顶部的表格中添加一个新行。

da.Fill(ds, "Disk")

插入此代码:

Dim dr As DataRow = ds.Tables("Disk").NewRow
dr(ValueField) = -1                    ' NB :use dr(0) in others situation see example 
dr(TextField) = "< Select Option >"    ' NB :use dr(1) in others situation see example 
ds.Tables("Disk").Rows.InsertAt(dr, 0) ' Insert the new row to datatable's top

就这样

例子 :

    Dim da As New SqlDataAdapter("Select id, Description FROM CelleEssiccazione", sConnessione)
    Dim ds As New DataSet
    da.Fill(ds, "Disk")

    Dim dr As DataRow = ds.Tables("Disk").NewRow
    dr("id") = 0                         ' or dr(0)
    dr("Description ") = "Seleziona..."  ' or dr(1)
    ds.Tables("Disk").Rows.InsertAt(dr, 0)

    MyComboBox.DataSource = ds.Tables("Disk")
    MyComboBox.DisplayMember = ds.Tables("Disk").Columns("Description").ToString
    MyComboBox.ValueMember = ds.Tables("Disk").Columns("id").ToString
于 2020-11-26T09:47:21.290 回答