In this article I will explain with an example, how to bind (populate)
ComboBox from
Database using
DataTable (DataSet) in
Windows Forms using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Form Design
The following Form consists of:
ComboBox – For capturing Name.
Button – The Button has assigned with Click event handler.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Bind (Populate) ComboBox from Database using DataTable (DataSet)
Inside the
Form1_Load event, the records from the database are fetched into a
DataTable.
Once the
DataTable is populated from database, a new row is inserted in First position which will act as the
Default (Blank) item for the
ComboBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.\SQL2014;Initial Catalog= AjaxSamples;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con))
{
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);
//Insert the Default Item to DataTable.
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "Please select";
dt.Rows.InsertAt(row, 0);
//Assign DataTable as DataSource.
cbCustomers.DataSource = dt;
cbCustomers.DisplayMember = "Name";
cbCustomers.ValueMember = "CustomerId";
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con)
'Fill the DataTable with records from Table.
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
'Insert the Default Item to DataTable.
Dim row As DataRow = dt.NewRow()
row(0) = 0
row(1) = "Please select"
dt.Rows.InsertAt(row, 0)
'Assign DataTable as DataSource.
cbCustomers.DataSource = dt
cbCustomers.DisplayMember = "Name"
cbCustomers.ValueMember = "CustomerId"
End Using
End Using
End Sub
Fetching the selected Text and Value of ComboBox
When the
Submit Button is clicked, the selected
Text and
Value of the
ComboBox is fetched and displayed using
MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "Name: " + cbCustomers.Text;
message += Environment.NewLine;
message += "CustomerId: " + cbCustomers.SelectedValue;
MessageBox.Show(message);
}
VB.Net
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim message As String = "Name:" & cbCustomers.Text
message += Environment.NewLine
message += "CustomerId:" & cbCustomers.SelectedValue
MessageBox.Show(message)
End Sub
Screenshot
Downloads