How to Bind CheckedListBox control in windows form application

In this article I will explain you how to bind  from database to CheckedListbox control  in windows forms application in asp.net.

first we need to create database for bind data to checkedlistbox control in windows application.

Here, we create database in sql server, database name is :Example

Create a Table in Database, Table name is : UserMst
Design table with 1 column named UserName.

UserName as nvarchar(50) datatype Allow null = Yes

After completion of design table enter some values in table manually for our example.

Now, design a windows for with one CheckedListbox control along with one Button control.

Checkedlistbox control in wondows application
How to Bind Data to Checkedlistbox control in wondows application

write below code on button click event for bind data from database to checkedlistbox control.

private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source='.\\SQLEXPRESS';Integrated Security='true';Initial Catalog='Example'");
SqlDataAdapter Adp = new SqlDataAdapter("SELECT UserName FROM UserMst", con);
DataTable DT = new DataTable();
Adp.Fill(DT);

for (int i = 0; i < DT.Rows.Count; i++)
{
checkedListBox1.Items.Add(DT.Rows[i]["UserName"].ToString());
}
}

Checkedlistbox control in wondows application
How to Bind Data to Checkedlistbox control in wondows application

Leave a Reply

Your email address will not be published. Required fields are marked *