LeonidasFett
LeonidasFett

Reputation: 3132

Custom data binding on ComboBox

I am trying to fill a ComboBox with multiple instances of a custom ComboBoxItem class. The ComboBoxItem class looks like this:

 class ComboBoxItem
 {
     public string Text { get; set; }
     public object Value { get; set; }

     public override string ToString()
     {
         return Text;
     }
 }

I can fill the CombBox and read it's values just fine. My only problem is when an existing item comes in, the values should be bound to my ComboBox. But I don't know how to tell the Binding that it should use ComboBoxItem.Value as Value field.

//what to put in place of "SelectedItem"??
comboBox.DataBindings.Add(new Binding("SelectedItem", row, "F_KundenId", true, DataSourceUpdateMode.OnPropertyChanged));

Upvotes: 1

Views: 5183

Answers (2)

LeonidasFett
LeonidasFett

Reputation: 3132

Ok, alternatively to Mangist's answer, I have come up with a solution of my own. No need for any custom class. Just use Dictionary<Key, Value>. The only thing is that you have to be careful WHEN during runtime you set DisplayMember and ValueMember. If you reset the DataSource of the ComboBox (set it to null and rebind it), then you need to also set the two Member properties. Then just bind the Dictionary<Key, Value> to the ComboBox using a BindingSource object.

private void InitCombos()
{
    Dictionary<string, int> items = GetItems();    
    combo.DisplayMember = "Key";
    combo.ValueMember = "Value";
    combo.DataSource = new BindingSource(items, null);
}

//This was where my problem was. I didn't set the two Member properties of my ComboBox, 
//thus preventing correct rebinding of DataSource
public void combo2_SelectedIndexChanged(object sender, EventArgs e)
{
    combo.DataSource = null;
    Dictionary<string, int> newItems = GetItems();    
    combo.DisplayMember = "Key";
    combo.ValueMember = "Value";
    combo.DataSource = new BindingSource(items, null);
}

Upvotes: 1

Jon
Jon

Reputation: 3255

This is how I bind all my Windows ComboBoxes in my app:

First use this to load your dataSource, in this case a List:

    public static void LoadComboBox(ComboBox comboBox, object dataSource, string valueMember, string displayMember)
    {
        comboBox.DataSource = dataSource;

        comboBox.ValueMember = valueMember;
        comboBox.DisplayMember = displayMember;
    }

Then use this to bind the selected value to your "row" column "F_KundenId":

    public static void BindComboBox(ComboBox comboBox, object boundDataSource, string boundDataMember)
    {
        comboBox.DataBindings.Clear();
        comboBox.DataBindings.Add("SelectedValue", boundDataSource, boundDataMember);
    }

And here is a helper method to do both in a single call:

public static void LoadAndBindComboBox(ComboBox comboBox, object dataSource, string valueMember, string displayMember,
    object boundDataSource, string boundDataMember)
{
    LoadComboBox(comboBox, dataSource, valueMember, displayMember);
    BindComboBox(comboBox, boundDataSource, boundDataMember);
}

This code can be used with ANY datasources you want, and can bind to any column of a DataTable, DataRow or object.

Example:

LoadAndBindComboBox(comboBox, myItems, "Value", "Text", row, "F_KundenId");

Upvotes: 3

Related Questions