Groups | Blog | Home
all groups > dotnet windows forms > october 2006 >

dotnet windows forms : ContextMenuStrip - DataBinding to one of the items



cisco
10/12/2006 11:36:02 AM
I'm trying to do basic data binding to a ToolStripComboBox but the
items aren't being populated. If i put a break point i can see that
the data source has information in it but it's not being displayed. So
the DataSource does have the BindingList associated with it. This is for a
ContextMenuStrip; a toolstrip seems to work ok.

private BindingList<string> _binding = new BindingList<string>(new
string[] { "Hello", "Goodbye" });

protected override void OnLoad(...) {
// i also tried going tdirectly to toolStripComboBox1.ComboBox
ComboBox cBox = toolStripComboBox1.Control as ComboBox;
toolStripComboBox1.Sorted = false;
cBox.Sorted = false;

cBox.DataSource = _binding;
this._cbo2.ComboBox.DataSource = _binding;
comboBox1.DataSource = _binding;


}

Googlin', the only thing i can find is a mention that if the data
source is an array, and you try to sort the combo box, it will cause
the items not to display. So i set the sorted properties to false for
the hell of it.


The only way i got it to work was to add the control to the form's/user
control's control collection, do the data bindnig, then add it to a
ToolStripControlHost. If i don't perform that step it doesn't work. The
problem with this approach is that when i add new items to the binding list
it won't update the combobox(or listbox) even if i call refresh on the
currency manager or call ResetBindings on the BindingList.

cisco
10/12/2006 3:12:01 PM
Bart,

Ahh that makes sense. The binding context was switched! argg! my head
hurts ;)

I really apprecite the help!

Cisco

[quoted text, click to view]
Bart Mermuys
10/12/2006 9:48:42 PM
Hi,

[quoted text, click to view]

Try explicitly setting a BindingContext first:

toolStripComboBox1.ComboBox.BindingContext = this.BindingContext;

I would also put a BindingSource inbetween:

toolStripComboBox1.ComboBox.DataSource = new BindingSource(_binding, "");


HTH,
Greetings

[quoted text, click to view]

AddThis Social Bookmark Button