adding a data bound combo box

adding a data bound combo box

Post by YmxhZGVjbG » Sat, 16 Aug 2008 04:35:05


Can a data bound combo box be added to a toolstrip such as a
bindingnavigator. I can add the toolstrip item, but the databinding
properties are not present. You also cannot drop a databound text on the
toolstrip.
 
 
 

adding a data bound combo box

Post by TW9ydGVuIF » Tue, 19 Aug 2008 14:29:01


You can databind against a ToolstripComboBox, but I'm not sure you can do it
in the designer, and in any case I find doing it in code to be much more
maintainable.

ToolstripComboBox does not derive from Control and therefore does not have
the databinding capabilities you find on a control. It does, however, have a
reference to the ComboBox displayed in its Control property.

ComboBox cb = toolStripComboBox1.Control as ComboBox;
cb.DataSource = CreateKeyValuePairList();
cb.DisplayMember = "Key";
cb.ValueMember = "Value";
cb.DataBindings.Add("SelectedValue", source, "SomeProperty");

--
Happy Coding!
Morten Wennevik [C# MVP]

 
 
 

adding a data bound combo box

Post by YmxhZGVjbG » Tue, 19 Aug 2008 20:22:01

Sorry, I was a little to quick to post, and forgot to add I am working with
VB. Thanks for the speedy reply, and I will try to figure the C to VB.

bladecleaver
 
 
 

adding a data bound combo box

Post by TW9ydGVuIF » Tue, 19 Aug 2008 21:02:01


The difference between C# and VB.Net is very small. Basically drop all ;
and change the order of object declaration :)

Dim cb As ComboBox = ToolStripComboBox1.Control
cb.DataSource = CreateKeyValuePairList()
cb.DisplayMember = "Key"
cb.ValueMember = "Value"
cb.DataBindings.Add("SelectedValue", source, "SomeProperty")

--
Happy Coding!
Morten Wennevik [C# MVP]
 
 
 

adding a data bound combo box

Post by YmxhZGVjbG » Wed, 20 Aug 2008 21:40:01

Thank you. That did the trick.