Dan Vogel
Dan Vogel

Reputation: 3938

Updating a databound ComboBox

I am having virtually the same problem as this:

C# Update combobox bound to generic list

However, I am trying to change the displayed strings; not add, remove, or sort. I have tried the BindingList solution provided in the referenced question, but it has not helped. I can see the combobox's DataSource property is correctly updated as I edit the items, but the contents displayed in the combobox are not those in the DataSource property.

my code looks as follows:

mSearchComboData = new List<SearchData>();
mSearchComboData.Add(new SearchData("", StringTable.PatientID));
mSearchComboData.Add(new SearchData("", StringTable.LastName));
mSearchComboData.Add(new SearchData("", StringTable.LastPhysician));
mSearchComboData.Add(new SearchData("", StringTable.LastExamDate));

mBindingList = new BindingList<SearchData>(mSearchComboData);

SearchComboBox.Items.Clear();
SearchComboBox.DataSource = mBindingList;
SearchComboBox.ValueMember = "Value";
SearchComboBox.DisplayMember = "Display";

...

When I try to update the content I do the following:

int idx = SearchComboBox.SelectedIndex;
mBindingList[idx].Display = value;
SearchComboBox.Refresh();

EDIT::

RefreshItems seems to be a private method. I just get the error message:

"'System.Windows.Forms.ListControl.RefreshItems()' is inaccessible due to its protection level"

ResetBindings has no effect.

Upvotes: 7

Views: 22516

Answers (3)

BrianB
BrianB

Reputation: 21

This is an old post but this may be useful.

I have just been looking at the same problem and have found that if you call ResetItem on the BindingList object, with the changed Items position, it internal raises the Itemchanged notification event for you causing the list to update.

int idx = SearchComboBox.SelectedIndex;
mBindingList[idx].Display = value;

mBindingList.ResetItem(idx); //raise Item changed event to update the list display

Upvotes: 2

YonahW
YonahW

Reputation: 16520

instead of SearchComboBox.Refresh();

try SearchComboBox.RefreshItems();

or SearchComboBox.ResetBindings();

I think it is really the latter that you need.

You can access the documentation for it's members here.

Upvotes: 2

BFree
BFree

Reputation: 103740

If you were to change the entire object, meaning your entire SearchData object, then the bindinglist would have knowledge of this change, and therefore the correct events would internaly get fired, and the combobox would update. HOWEVER, since you're only updating one property, the bindinglist has no idea that something has changed.

What you need to do is have your SearchData class implement INotifyPropertyChanged. Here's a quick sample I wrote to demonstrate:

public class Dude : INotifyPropertyChanged
    {
        private string name;
        private int age;

        public int Age
        {
            get { return this.Age; }
            set
            {
                this.age = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                }
            }
        }
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;


    }

And here's some code to test:

        private void button1_Click(object sender, EventArgs e)
        {
            //Populate the list and binding list with some random data  
            List<Dude> dudes = new List<Dude>();
            dudes.Add(new Dude { Name = "Alex", Age = 27 });
            dudes.Add(new Dude { Name = "Mike", Age = 37 });
            dudes.Add(new Dude { Name = "Bob", Age = 21 });
            dudes.Add(new Dude { Name = "Joe", Age = 22 });

            this.bindingList = new BindingList<Dude>(dudes);
            this.comboBox1.DataSource = bindingList;
            this.comboBox1.DisplayMember = "Name";
            this.comboBox1.ValueMember = "Age";

        }


    private void button3_Click(object sender, EventArgs e)
    {
        //change selected index to some random garbage
        this.bindingList[this.comboBox1.SelectedIndex].Name = "Whatever";
    }

Since my class now implements INotifyPropertyChanged, the binding list gets "notified" when something changes, and all this will thus work.

Upvotes: 11

Related Questions