如何在VB.Net中填充Combobox,这些项目来自DataSet ...这是我的代码
Dim LibDSPopulate As New DataSet
Dim LibDAPopulate As OdbcDataAdapter = New OdbcDataAdapter("SELECT DISTINCT Category FROM tblBooks", LibConn)
LibDAPopulate.Fill(LibDSPopulate, "tblBooks")
cmbCategoryF.Items.Add(LibDSPopulate)
答案 0 :(得分:1)
您可以使用数据绑定facility。
'Add an empty entry
Dim dr As DataRow = LibDSPopulate.Tables("tblBooks").NewRow
dr("Category") = "***Select***"
LibDSPopulate.Tables("tblBooks").Rows.InsertAt(dr, 0)
cmbCategoryF.DataSource=LibDSPopulate.Tables("tblBooks")
cmbCategoryF.DisplayMember="Category" 'Name of field
cmbCategoryF.ValueMember="Category" 'Name of field
如果您不想使用databindng,请使用Items.Add()方法添加每个项目。
For Each row As DataRow In LibDSPopulate.Tables("tblBooks").Rows
ComboBox1.Items.Add(row("Category"))
Next