将额外的项添加到数据绑定组合框中

时间:2012-07-13 09:40:34

标签: vb.net data-binding combobox

我正在尝试将项目与数据库绑定后添加到组合框中,这样组合框就可以这样:

组合框

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我使用BindingSource对象,我连接到源列表,然后将它tye到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

结束班

答案 2 :(得分: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