ComboBox显示系统.data.datarowview vb.NET

时间:2015-05-08 07:50:59

标签: vb.net

当我想选择一个酒店名称时,我想为每个酒店名称分配一个房间数

sqlCmd1 = "Select ROID from Room"
        ad = New OleDb.OleDbDataAdapter(sqlCmd1, cnn)
        ds = New DataSet
        ad.Fill(ds, "Room")
        For Each row As DataRow In ds.Tables(0).Rows
            roomCB.Items.Add(row.Item(0))
        Next

这是酒店的ComboBox

 Private Sub hotelCB_SelectedIndexChanged(sender As Object, e As EventArgs) 
                                           Handles hotelCB.SelectedIndexChanged    
        If (hotelCB.SelectedIndex = 0) Then
            sqlCmd1 = "Select ROID from Room"
            ad = New OleDb.OleDbDataAdapter(sqlCmd1, cnn)
            ds = New DataSet
            ad.Fill(ds, "Room ID")
            roomCB.DataSource = ds.Tables(0).DefaultView
        ElseIf (hotelCB.SelectedIndex = 1) Then
            sqlCmd1 = "Select ROID from Room where HotelID=" + 
                                                hotelCB.SelectedIndex.ToString()
            ad = New OleDb.OleDbDataAdapter(sqlCmd1, cnn)
            ds = New DataSet
            ad.Fill(ds, "Room ID")
            roomCB.DataSource = ds.Tables(0).DefaultView
        End If
    End Sub

2 个答案:

答案 0 :(得分:0)

您必须指定DisplayMember

roomCB.DisplayMember = "ROID";
roomCB.ValueMember = "ROID";
roomCB.DataSource = ds.Tables(0)

答案 1 :(得分:0)

将您的ComboBox加载逻辑更改为

    sqlCmd1 = "Select ROID from Room"
    ad = New OleDb.OleDbDataAdapter(sqlCmd1, cnn)
    ds = New DataSet
    dt = New DataTable
    ad.Fill(ds, "Room")
    dt=ds.Tables(0)
    For i as integer=0 to dt.Rows.Count - 1
        roomCB.Items.Add(dt.Rows(i).Item(0))
    Next