Bind a dataset to a combo box in C#

The DataSet is a memory-resident representation of data that offers a consistent relational programming model, irrespective of the data source. It provides a structured way to store and manage data, including tables, relationships, and constraints.

The SqlDataAdapter object allows us to populate DataTables within a DataSet. It facilitates retrieving data from a data source and populating the DataTables using methods like Fill(), providing seamless integration of data into the DataSet.

Bind data source to ComboBox

dataset-combobox.jpg

The ComboBox control allows the user to select an option from a list. We can populate a ComboBox from the values in a DataSet, providing a user-friendly interface for selecting options. This is often done by binding the ComboBox to the DataSet's values.

comboBox1.DataSource = ds.Tables[0]; comboBox1.ValueMember = "au_id"; comboBox1.DisplayMember = "au_lname";

Select Item from ComboBox

comboBox1.SelectedValue

The following C# program retrieve the values from database and store it in a dataset and later bind to a combobox.

using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string connetionString = null; SqlConnection connection; SqlCommand command; SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); int i = 0; string sql = null; connetionString = "Data Source=.;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; 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 (Exception ex) { MessageBox.Show("Can not open connection ! "); } } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(comboBox1.Text + " " + comboBox1.SelectedValue); } } }

Binding a ComboBox to an Enum values

Enums in C# provide a convenient way to define a group of related constants that can be represented as strings or integer values. Enums allow for easy selection of options by representing them as a list of string values, which can be bound to a ComboBox for user selection.

Data Binding an Enum with Descriptions

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

The follwoing C# program bind a combobox with Enum values.

using System; using System.Windows.Forms; using System.Collections.Generic; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public enum Colors { Red = 10, Blue = 20, Green = 30, Yellow = 40, } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(comboBox1.Text + " " + Convert.ToString((int)comboBox1.SelectedValue)); } private void Form1_Load(object sender, EventArgs e) { comboBox1.DataSource = Enum.GetValues(typeof(Colors)); comboBox1.SelectedItem = Colors.Green; } } }

Bind a ComboBox to a generic Dictionary

The Dictionary class in C# is a data structure that represents a collection of key-value pairs. Each key in the dictionary is unique, and it can have at most one associated value. However, a value can be associated with multiple keys. Dictionaries are useful for storing and accessing data based on unique keys.

Dictionary as a Combobox Datasource

comboBox1.DataSource = new BindingSource(colors, null); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key";

The following C# program populating a Combobox from a Dictionary .

Full Source C#
using System; using System.Windows.Forms; using System.Collections.Generic; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(Convert.ToString(comboBox1.Text + " " + comboBox1.SelectedValue)); } private void Form1_Load(object sender, EventArgs e) { var colors = new Dictionary < string, string > (); colors["10"] = "Red"; colors["20"] = "Blue"; colors["30"] = "Green"; colors["40"] = "Yellow"; comboBox1.DataSource = new BindingSource(colors, null); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key"; } } }