Print Page | Close Window

Binding Combo Boxes

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=463
Printed Date: 20-Sep-2025 at 4:25am


Topic: Binding Combo Boxes
Posted By: Bill Jensen
Subject: Binding Combo Boxes
Date Posted: 02-Oct-2007 at 3:45pm
In earlier correspondence, Dan asked:
 
How do I bind a ComboBox list to a constant list of values?
 
Bill J. Replied:
 
If you're using a DevEx ComboBoxEdit control, just leave the ListConverter data source blank.  In the designer view in the control's properties, select the "Edit Items" link at the bottom and type in the constant strings you want to appear in the list.
 
In the Dot Net Combo Box, there is an Items property with a collection editor to do the same thing.
 
I think this will work as long as the combo is bound to a string-valued property.
 
Dan replied:
 
I tried that but it came back blank.
 
What I did was add a string array in my presenter and then bound the combobox to that. Works like a charm.
 
The only problem I have is that I get the red exclamation next to it and it doesn't bind properly.
 
Value that is suppose to bind is the same as the values in the combobox.



Replies:
Posted By: Bill Jensen
Date Posted: 02-Oct-2007 at 3:58pm

I've tried this myself:

I added a string-valued property named "Direction" (and a PropertyDescriptor) to my Employee class.
 
Using the ControlBindingManager I bound the Direction property to a (DotNet) ComboBox, leaving the ListConverter configuration at default values.
 
I added a constant named "Direction" to ListConverterNames (in the Interface project)
 
In the ListConverterService (in the Foundation module) I added a new case for "Direction" that calls MakeDirectionConverter() to CreateServiceItemCore().
 
I added the method:
 

/// <summary>Get the global ListConverter for all TitlesOfCourtesy.</summary>

private static ListConverter MakeDirectionConverter()

{

ListConverter converter =

new ListConverter(typeof(string), Editability.Optional);

converter.ListSource = new BindingSource(mDirections, null);

converter.DisplayMember = "__Self";

return converter;

}

private static string[] mDirections = new string[] { "North", "South", "East", "West" };

to the ListConverterService.

Finally (!) I added this code to the presenter for my view:

/// <summary>Bind to the Direction List Control</summary>

/// <remarks>

/// Adds the proper ListConverter for Direction.

/// </remarks>

private void BindDirection() {

SetBindingListConverter(Employee.Descriptors.Direction, ListConverterNames.Direction);

}

This works--the list appears properly and the selected value is bound to the Direction property of the Employee entity.
 
Whew!  Thats quite a bit of stuff, though most of the complexity has to do with getting the ListConverter from a service, CAB style.  I'll ask Ward if he can recommend a simpler mechanism.
 
Bill


Posted By: Linguinut
Date Posted: 02-Oct-2007 at 4:03pm
I'd highly suggest utilizing the ListConverterService.  You could create your string list in the GetMyListBindingSource method (returns a http://www.ideablade.com/techtip_thebindablelist.htm - BindableList ).  The strings could technically come from anywhere...database, xml file, text file, etc.  Or, just hard code them.  Then, if you need that list elsewhere...it's all ready to go.


Posted By: Bill Jensen
Date Posted: 10-Oct-2007 at 11:49am
Finally got a chance to run this by Ward.  This is in fact the best (and most Cabanaesque) solution.  So I stand by it.
 
And I agree with Bill C.--the ListConverterService is the way to go.
 
Bill J.


Posted By: orcities
Date Posted: 10-Oct-2007 at 1:40pm
Thanx Bill that did it. I wonder what I am doing wrong with my other list boxes.



Print Page | Close Window