Suppose the ComboBox elements are elements of some List that I get from the database and bind to the ComboBox through the DataSource .

Is it possible to add an element to the ComboBox that will be present there regardless of whether it is in the DataSource or not?

For example, if ComboBox uses as some kind of filter, then the "All" option would be useful to me.

It turns out it every time hands need to be added to DataSource ?

  • WPF or WinForms? - VladD
  • @VladD, WinForms. It is necessary to make it so that in the ComboBox there is such an element that would not disappear when the DataSource is connected. - iluxa1810
  • With WinForms, I have no experience, maybe someone else will catch up. - VladD
  • What's wrong with getting a List simply adding an All element to the beginning? - ExiD
  • @ExiD, I do that. But I thought there was some other option. - iluxa1810

1 answer 1

The option of manually adding the “All” entry is the fastest and easiest.

There is a more complicated option - create a heir ComboBox , which when you change the collection of elements adds "Everything" automatically.

You, unfortunately, still torment WinForm , I don’t remember ready solutions in it right away.

The solution to the forehead is simple:

  public class ComboBoxWithAll : ComboBox { public ComboBoxWithAll() { this.BindingContextChanged += OnBindingContextChanged; } private void OnBindingContextChanged(object sender, EventArgs args) { var item = "Все"; var list = this.DataSource as List<string>; if (list != null && list.Any() && list[0] != item) { list.Insert(0, item); } OnDataSourceChanged(args); } } 

But it should be borne in mind that I made explicit strings to the DataSource type here and I change it, which in principle should never be done by controls.

If this does not suit you, then you need to bother even more, with the division into a real collection and a displayed one, and then you should add a sign that the "All" item is selected.