Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 660
Dan Vogel Avatar asked Feb 02 '09 19:02

Dan Vogel


People also ask

How do I bind a ComboBox?

To bind a ComboBox or ListBox control If you are binding to a table, set the DisplayMember property to the name of a column in the data source. If you are binding to an IList, set the display member to a public property of the type in the list.


3 Answers

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.

like image 84
BFree Avatar answered Oct 08 '22 13:10

BFree


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.

like image 38
YonahW Avatar answered Oct 08 '22 12:10

YonahW


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
like image 2
BrianB Avatar answered Oct 08 '22 11:10

BrianB