In this article I will explain with an example, how to bind (populate) CheckedListBox from Database using DataTable (DataSet) in Windows Forms (WinForms) Application 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 Form consists of a CheckedListBox control and a Button. The Button has been assigned Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Bind (Populate) CheckedListBox from Database using DataTable (DataSet)
Inside the Form Load event, the records from the database are fetched into a DataTable.
Once the DataTable is populated from database, it is assigned to the DataSource property of the CheckedListBox.
The Name field is set in the DisplayMember property and the CustomerId field is set in the ValueMember property of the CheckedListBox.
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);
//Assign DataTable as DataSource.
clbCustomers.DataSource = dt;
clbCustomers.DisplayMember = "Name";
clbCustomers.ValueMember = "CustomerId";
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.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)
'Assign DataTable as DataSource.
clbCustomers.DataSource = dt
clbCustomers.DisplayMember = "Name"
clbCustomers.ValueMember = "CustomerId"
End Using
End Using
End Sub
Fetching the Text and Value part of the Selected Items of CheckedListBox
When the Button is clicked, a loop is executed and the Text and Value part of all the Selected Items of the CheckedListBox control are fetched and displayed using MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "";
foreach (object item in clbCustomers.CheckedItems)
{
DataRowView row = item as DataRowView;
message += "Name: " + row["Name"];
message += Environment.NewLine;
message += "CustomerId: " + row["CustomerId"];
message += Environment.NewLine;
message += Environment.NewLine;
}
MessageBox.Show(message);
}
VB.Net
Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
Dim message As String = ""
For Each item As Object In clbCustomers.CheckedItems
Dim row As DataRowView = TryCast(item, DataRowView)
message += "Name: " & row("Name")
message += Environment.NewLine
message += "CustomerId: " & row("CustomerId")
message += Environment.NewLine
message += Environment.NewLine
Next
MessageBox.Show(message)
End Sub
Screenshot
Downloads