从mySQL DB循环添加为ComboBox项分配值

时间:2012-04-20 13:45:38

标签: vb.net combobox

所以我能够使用正确的“clientLocations”列表填充comboBox。

但是它们的值都是0;因此,手头的问题是将comboBox项的值正确地分配给数据库列(在数据集中返回)而不仅仅是名称。

Private Sub updateClientLocationComboBox()
    'clear list
    comboBox_clientLocations_deviceList.Items.Clear()
    'get dataset from database
    Dim ds As DataSet = GetClientLocations(_objHost, CInt(comboBox_clients.SelectedValue))

    'force insert an "All Locations" datarow
    Dim allClientRow As DataRow = ds.Tables(0).NewRow
    allClientRow(0) = 0
    allClientRow(1) = "--All Locations--"
    ds.Tables(0).Rows.InsertAt(allClientRow, 0)

    'Check for table in dataset; if exist loop and populate comboBox
    If Not ds Is Nothing AndAlso ds.Tables.Count > 0 Then
        For Each A As DataRow In ds.Tables(0).Rows
            comboBox_clientLocations_deviceList.Items.Add(A("Name").ToString)
        Next
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

如果我正确地读你,我想你想用你的DataSet做这个:

If Not ds Is Nothing AndAlso ds.Tables.Count > 0 Then
    With comboBox_clientLocations_deviceList
        .DataSource = ds.Tables(0)
        .DisplayMember = "Name"
        .ValueMember = "WhateverCol(0)IsCalled"
    End With
End if

然后,使用comboBox_clientLocations_deviceList,SelectedText和SelectedValue属性应该可以工作。