Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Combobox list from a Collection in another control
 

Combobox list from a Collection in another control

Hi,

What I'm trying to do is populate a set of values in a combobox based on the items checked in a checked list box. In the setup of my form I set the combobox.DataSource = checkedlistbox.CheckedItems property. This works well and populates the combobox with what is currently ticked.

The problem then comes with updates, the list doesn't update when the list of checked items changes. The only way I've been able to force it to relist them so far is by setting the DataSource to nothing and then back again, this of course does more than simply refreshing the list and I figure there has to be a better way.

Does anyone have any ideas ?

Many thanks in advance,

Neil.
Neil Moore  Monday, February 11, 2008 1:57 PM

Hi

The reason why the ComboBox doesn’t update the data source automatically is that the CheckedItemCollection class doesn’t implement the IBindingList interface, which has a ListChanged event to notify the ComboBox to update the data source if the underlying list get changed. If you want to update the ComboBox’s data source automatically when the list of checked items changes, you can custom a CheckedListBox like the following:

Code Snippet

class MyCheckedListBox : CheckedListBox

{

BindingList<object> list = new BindingList<object>();

public new BindingList<object> CheckedItems

{

get

{

return list;

}

}

protected override void OnItemCheck(ItemCheckEventArgs ice)

{

base.OnItemCheck(ice);

if (ice.NewValue != CheckState.Unchecked)

{

list.Add(this.Items[ice.Index]);

}

else

{

list.Remove(this.Items[ice.Index]);

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Friday, February 15, 2008 7:49 AM

Hi

The reason why the ComboBox doesn’t update the data source automatically is that the CheckedItemCollection class doesn’t implement the IBindingList interface, which has a ListChanged event to notify the ComboBox to update the data source if the underlying list get changed. If you want to update the ComboBox’s data source automatically when the list of checked items changes, you can custom a CheckedListBox like the following:

Code Snippet

class MyCheckedListBox : CheckedListBox

{

BindingList<object> list = new BindingList<object>();

public new BindingList<object> CheckedItems

{

get

{

return list;

}

}

protected override void OnItemCheck(ItemCheckEventArgs ice)

{

base.OnItemCheck(ice);

if (ice.NewValue != CheckState.Unchecked)

{

list.Add(this.Items[ice.Index]);

}

else

{

list.Remove(this.Items[ice.Index]);

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Friday, February 15, 2008 7:49 AM

Hi,

That worked a treat.

Many thanks,

Neil.

Neil Moore  Friday, February 15, 2008 3:46 PM

You can use google to search for other answers

Custom Search

More Threads

• combining datagridview
• DataGridView BindingSource Change Position
• how to catch row modification hwn user move to a dfferent row for windwos form detail view
• .Net: Using VB to bind controls to a dataset row/column
• InkOverlay.Renderer.Draw Method not working with dotnet obfuscator
• Maintaining the selected row after sorting a DataGridView
• Cann't open Report Data Source
• Cancel listview selecting an item
• Polymorphic data bound to a DatagridView
• DataGridView Selection Mode