views:

453

answers:

1

I use a ComboBox which is bound to a List<> of Entities. How can I add a "Not selected" entry to the combobox? Adding null to the list results in empty combobox.

A: 

You should use an empty string or other unique text pattern instead of null.

And then You can handle the Format event of the Combobox to intercept the <empty> and display an alternate text.

private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
   e.Value = FormatForCombobox(e.ListItem);
}


private string FormatForCombobox(object value)
{
  string v = (string) value;
  if (v == string.Empty)
     v = "<no Selection>";
  return v;
}
Henk Holterman
I can't add anything to the combobox because it is bound to the list of entities.
wRAR
You can still add an event when it's bound.
Henk Holterman
It won't help because I have nothing special to format.
wRAR
You have to add something special first...
Henk Holterman
I can't add anything to the combobox because it is bound to the list of entities.
wRAR
You add a special item to the List<> and an eventhandler to the Combobox.
Henk Holterman
That special item should be entity object. So how do I create a fake entity object? What entity key should it have?
wRAR
Yes, try to create a special entity, the Key should be an recognizable (invalid) value. If that doesn't work you may need to copy entities to the .Items, and again use Format
Henk Holterman
Could you possibly extend the combo box class? The binding happens behind the scenes, I wonder if you could override those methods that populate it.. Might be a bit difficult. I wouldn't recommend creating an empty entity here and there... my 2 cents.
lb