I am developing a Winform and I need a checkedlistbox. I have the values stored in an object which has a List property:
public static class Fields
{
public static IList<string> FieldList { get; set; }
static Fields()
{ ...//populate FieldList }
}
Now I would like my CheckedListBox to use Fields.FieldList as datasource. After searching online I found I needed to set
//in myForm_Load
mycheckedListBox.DataSource = Fields.FieldList;
But myCheckedListBox does not have a DataSource property.
Am I missing something here?
Per the documentation, it should have this property... http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.datasource(VS.90).aspx
However, I also had the same issue on a project a while back, and used this CodeProject article to code the solution in the one project where I needed this feature.
Researching a bit more, I did find this:
http://connect.microsoft.com/VisualStudio/feedback/details/115199/checkedlistbox-datasource-displaymember-valuemember-are-hidden
Edit: The above link is no longer working, but the exceprt below is from the article that once resided there.
Posted by Microsoft on 5/30/2005 at 10:28 AM
Thanks for the feedback however this is by design. We do not support data binding on the CheckedListBox control. These properties are inherited from it base class and cannot be removed so we hid them form the property grid and IntelliSense.
That explains why the property exists, but doesn't show in Intellisense.
This blog post is worth a read as well: http://waxtadpole.wordpress.com/2009/10/12/assigning-custom-class-to-checkedlistbox-datasource/
Here is how I am binding a List<T>
of User
objects into CheckedListBox.
((ListBox)myCheckedListBox).DataSource = listOfUsers;
((ListBox)myCheckedListBox).DisplayMember = "FullName";
((ListBox)myCheckedListBox).ValueMember = "UserID";
Of course this is not recommended, since documentation is telling us that this property is hidden.
The code above works, but I noticed some side effects in Visual Studio 2012 such as:
Delay for rendering checked marker:
After you click on the desired item, there is annoying delay to render the "checked" marker.
In my case, CheckOnClick
property is True, CausesValidation
is False.
Personally I use a DataGridView
that is bound to a DataTable
that has a Boolean field along with a field for the display value.
If you hide the column headers and row headers then you get something pretty close to what a CheckedListBox
gives you.
This can be worked around by iterating through your would-be datasource and adding its items one-at-a-time. For example:
This, which will cause an exception:
myCheckedListBox.DataSource = myStringList;
Can be modified to this:
foreach (string myString in myStringList)
{
myCheckedListBox.Items.Add(myString);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With