Click here to Skip to main content
15,867,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,How to bind database table to checkbox list in C#.net Windows application.


Table
----------------------
Id Name
----------------------
1 product1
2 product2
3 product3
------------------------
display member as-- name and value member as-- id
Posted
Updated 26-Apr-16 0:26am

 
Share this answer
 
Quote:
Bind checkbox list using datatable column
Please check the below Link


http://www.dotnetspider.com/forum/316985-Bind-checkbox-list-using-datatable-column.aspx[^]
 
Share this answer
 
C#
dal dl = new dal();//Class name
 DataSet ds = new DataSet();

string query = "SELECT [emp_id],[emp_role_id],[emp_name],[email] ,[contact]  FROM [tb_employee]";
           ds = dl.fetchrecord(query);

           if (ds.Tables[0].Rows.Count > 0)
           {
               foreach (DataRow item in ds.Tables[0].Rows)
               {
                   checkedListBox1.Items.Add(item["emp_name"].ToString());
               }
           }
 
Share this answer
 
Hi sanoth,

Bind CheckedListBOx or any other control with displaymember and valuemember is quite simple you just need to specify datasource property of control as well as displaymember and valuemember.


Following is working code 100 % work for me i have tested:P

/* checkedlistbox bindig code */

DataSet ds = new DataSet();

string strChechboxlist = "select Subject_ID as code, SubjectName as Display from dbo.Mst_Subject_Detail";

/* filldataset() is function i have created to return dataset. */
ds = dc.FillDataSet(strChechboxlist);
if (ds.Tables[0].Rows.Count > 0)
{
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.DisplayMember = "Display";
checkedListBox1.ValueMember = "code";

}

/* for fetching valuemember or displaymember from checkedlistbox */


for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{

/*Now with the following code we can get valemember and displaymember as per your requirement you can store in table. */
DataRow r;
r = ((DataRowView)this.checkedListBox1.CheckedItems[i]).Row;
string val = (r[this.checkedListBox1.ValueMember]).ToString();
string dis = (r[this.checkedListBox1.DisplayMember]).ToString();
r = null;

}


Note :- I am attaching working demo of code
 
Share this answer
 
Comments
Deepu S Nair 18-Jan-15 1:26am    
why are you answering old questions?
private void BindChackBox()
{
try
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from tblResion", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
checkedListRegion.Items.Add(dt.Rows[i]["Resion"].ToString());
}
}
catch { }
}
 
Share this answer
 
Comments
CHill60 26-Apr-16 7:52am    
This question was asked and answered over 2 years ago. Your solution adds nothing new to the discussion and includes less information than most. I advise you to be careful when answering old questions - make sure your contribution is new, relevant and correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900