CheckedListbox in C# 2005
I ' m using checkedlistbox in VS 2005 as I used to do in VS 2003.
But I no longer have checkedlistbox.DataSource to bind a datasource to this control. I would like to bind a datable to this control so that I can easily retrieve which datavalue has been been checked. Is there any way to obtain this again ?
Thanks in advance
Kriske
Answers
Hi,
Here I assume you mean the Winform CheckedListBox.
You may try to run the code below in a VS.NET 2005 winform application.
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("StringType", typeof(String));
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[0] = "TestString";
dt.Rows.Add(dr);
this.checkedListBox1.DataSource = dt;
this.checkedListBox1.DisplayMember = "StringType";
}CheckedListBox.DataSource Property
http://msdn2.microsoft.com/en-US/library/system.windows.forms.checkedlistbox.datasource(VS.80).aspxThis property supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Gets or sets the data source for the control. This property is not relevant for this class.
Version Information.NET Framework
Supported in: 2.0, 1.1, 1.0If you still have any concern, please feel free to post here.
Best regards,
Peter Huang
All Replies
Hi,
Here I assume you mean the Winform CheckedListBox.
You may try to run the code below in a VS.NET 2005 winform application.
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("StringType", typeof(String));
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[0] = "TestString";
dt.Rows.Add(dr);
this.checkedListBox1.DataSource = dt;
this.checkedListBox1.DisplayMember = "StringType";
}CheckedListBox.DataSource Property
http://msdn2.microsoft.com/en-US/library/system.windows.forms.checkedlistbox.datasource(VS.80).aspxThis property supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Gets or sets the data source for the control. This property is not relevant for this class.
Version Information.NET Framework
Supported in: 2.0, 1.1, 1.0If you still have any concern, please feel free to post here.
Best regards,
Peter Huangin Visual Studio 2005, the checkedlistbox control doesnt have a DataSource property.
Basically, I am just trying to populate a checked list box with a sql query. So far I have been unsuccessful. Can anyone show me an example using a simple select sql query?
thanks
DataSet
ds = new DataSet();ds =
Class1.getds("select * from Customers"); //Gets the dataset. for (int i = 0; i < ds.Tables[0].Columns.Count; i++){
checkedListBox1.Items.Add(ds.Tables[0].Columns
.ColumnName);}
That I've done, what I'm having troubles with is getting the selected items back to a string so I can use in sql sentence.
HI,
Did you have any luck with this answer. I am looking for the same thing myself. How do I bind the data and then make them correspond to an ID in the database and then automaticallym check based on the employee I am updating.
Do I bind the data one by one as above by putting in position numbers and then give them a corresponding ID somehow?
Thank you . I'm panicking.
Yes, the above solution worked for me. You should try it out and see if it conforms to what you want, and if it doesnt, if you post your exact problem, someone might be able to help you out.
I'm sure you all have figured this out by now but the DataSource, DisplayMember, and ValueMember properties are all available for a CheckedListBox in C#.NET 2005. It doesn't come up in intellisense but you can still use it. I was writing all of this additional code and then I tried just setting those three properties to a datatable and two of it's columns. Wala, it worked. I posted some code below to help.
DataTable myTable = new DataTable();
myTable = className.loadDataTable();
clstReportTypes.DataSource = myTable;
clstReportTypes.DisplayMember = "ReportType";
clstReportTypes.ValueMember = "ReportTypeID";
You can retrieve the ID value of each checked item using the code below
foreach (DataRowView myRow in clstReportTypes.CheckedItems)
{
do something with myRow[0].ToString();
}
- Dude! I've been searching for days trying to figure this out and here you have solved the problem in one fell swoop. Way to go and thanks!
- LOL!! LOL!! You called him DUDE!!!!
Good stuff man, I was not looking forward to figuring this out!!
Cheers!!


