Aaron
Aaron

Reputation: 1989

How to populate the Combobox in VBdotNet

How to pupolate the Combobox in VB.Net, the items are from DataSet... Here's my code

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)

Upvotes: 0

Views: 1217

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94625

You may use Data binding 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

If you don't want to use databindng then add each item using Items.Add() method.

 For Each row As DataRow In LibDSPopulate.Tables("tblBooks").Rows
    ComboBox1.Items.Add(row("Category"))
 Next

Upvotes: 1

Related Questions