Net-informations.com
SiteMap  | About    

How to populate a combo box from a dataset in VB.Net

dataset-combobox.jpg

The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects. The VB.Net SqlDataAdapter object allows us to populate Data Tables in a DataSet. Also we can populate a combo box from a dataset values.

Bind data source to ComboBox

ComboBox1.DataSource = ds.Tables(0) ComboBox1.ValueMember = "au_id" ComboBox1.DisplayMember = "au_lname"

Select Item from ComboBox

comboBox1.SelectedValue

The following VB.Net program fetch the values from database and store it in a dataset and later bind to a combobox.

Imports System.Data Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim connetionString As String = Nothing Dim connection As SqlConnection Dim command As SqlCommand Dim adapter As New SqlDataAdapter() Dim ds As New DataSet() Dim i As Integer = 0 Dim sql As String = Nothing connetionString = "Data Source=ServerName;Initial Catalog=databasename;User ID=userid;Password=yourpassword" sql = "select au_id,au_lname from authors" connection = New SqlConnection(connetionString) Try connection.Open() command = New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(ds) adapter.Dispose() command.Dispose() connection.Close() ComboBox1.DataSource = ds.Tables(0) ComboBox1.ValueMember = "au_id" ComboBox1.DisplayMember = "au_lname" Catch ex As Exception MessageBox.Show("Can not open connection ! ") End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(ComboBox1.Text + " -- " + ComboBox1.SelectedValue) End Sub End Class

Binding a ComboBox to an Enum in VB.Net

How to populate a combo box from Enum in VB.Net

Enum provide a better way in which to create a group of related constants that can be grouped as strings or integer values. When these values are bind to a ComboBox, the end user can select an option directly from a list of the Enum strings.

Data Binding an Enum with Descriptions

comboBox1.DataSource = Enum.GetValues(typeof(Colors)); comboBox1.SelectedItem = Colors.Green;

The follwoing VB.Net program bind a combobox with Enum values.

Public Class Form1 Public Enum Colors Red = 10 Blue = 20 Green = 30 Yellow = 40 End Enum Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.DataSource = [Enum].GetValues(GetType(Colors)) ComboBox1.SelectedItem = Colors.Green End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(ComboBox1.Text + " " + Convert.ToString(CInt(ComboBox1.SelectedValue))) End Sub End Class

Bind a ComboBox to a generic Dictionary in VB.Net

How to populate a combo box from Dictionary in VB.Net

A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.

Dictionary as a Combobox Datasource

ComboBox1.DataSource = New BindingSource(colors, Nothing) ComboBox1.DisplayMember = "Value" ComboBox1.ValueMember = "Key"

The following VB.Net program populating a Combobox from a Dictionary .




Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim colors = New Dictionary(Of String, String)()
        colors("10") = "Red"
        colors("20") = "Blue"
        colors("30") = "Green"
        colors("40") = "Yellow"
        ComboBox1.DataSource = New BindingSource(colors, Nothing)
        ComboBox1.DisplayMember = "Value"
        ComboBox1.ValueMember = "Key"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(Convert.ToString(ComboBox1.Text + " " + ComboBox1.SelectedValue))
    End Sub
End Class




net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.