.NET WinForms combobox bindingsource and databinding question
Asked Answered
D

2

1

I created the following class to model a person:

namespace DataBindingTest
{
    public enum colorEnum
    {
        Red,
        Green,
        Yellow,
        Blue,
    }

    class Person
    {
        private string _Name;
        private int _Age;
        private colorEnum _FavoriteColor;
        private bool _HasAllergies;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public int Age
        {
            get { return _Age; }
            set { _Age = value; }
        }

        public colorEnum FavoriteColor
        {
            get { return _FavoriteColor; }
            set { _FavoriteColor = value; }
        }

        public bool HasAllergies
        {
            get { return _HasAllergies; }
            set { _HasAllergies = value; }
        }

    }
}

On my main form, I have a combobox that will be bound to an array of Person objects. When I select a person from this combobox, I want to display their age (in a NumericUpDown control), whether they have allergies (as a checkbox) and their favorite color (in another combobox with DropDownStyle set to DropDownList). To accomplish this, I have:

  1. Added a comboBox (comboBoxPeople), a NumericUpDown control, a checkBox and another comboBox (comboBoxFavoriteColor) to my form.
  2. Created a new DataSource from my Person class declared above
  3. Added a BindingSource to my form
  4. Set the DataSource property of the BindingSource to the DataSource defined in #2.
  5. Set the DataSource for comboBoxPeople to the BindingSource and the DisplayMember to the Name property of the BindingSource
  6. I have bound the Age property of the BindingSource to a NumericUpDown control and the HasAllergies property of the BindingSource to a checkBox control
  7. In my constructor, I have created an array of 3 Person objects, defined all of their properties and then set the DataSource property of the BindingSource to this array

Thus far, everything is working as expected. Now I'd like to display the person's favorite color (i.e., the FavoriteColor property of the BindingSource) in comboBoxFavoriteColor. I've set the DropDownStyle to DropDownList since FavoriteColor is an enum. However, I'm unclear as to how I should bind this comboBox in order for it to 1) contain the FavoriteColor enum values and 2) have the appropriate color set as the SelectedItem when I choose a person from comboBoxPeople. Can anyone give me an idea on this? Thanks very much!

Disturb answered 1/8, 2011 at 1:15 Comment(0)
H
2

You could do as Tom suggests, but there's at least one easy way to do it without changing Person.FavoriteColor to a string.

Add a property to Person called FavoriteColorString:

public class Person 
{
    [...]
    public colorEnum FavoriteColor { get; set; }
    public string FavoriteColorString
    {
        get { return FavoriteColor.ToString(); }
        set { FavoriteColor = (colorEnum)Enum.Parse(typeof(colorEnum), value);  }
    }
}

Recompile so the new property shows up in the bindingsource.

Now bind comboBoxFavoriteColor.SelectedItem to FavoriteColorString.

And at runtime, do as Tom said:

comboBoxFavoriteColor.DataSource = Enum.GetNames(typeof(colorEnum));

Voila! It should now work the way you want.

When you persist the settings objects, just don't persist the FavoriteColorString property.

Hugohugon answered 1/8, 2011 at 3:9 Comment(5)
That works exactly the way I want it to! :-) And that's twice in the same weekend that you've given me a perfect explanation for something I was trying to do. My sincere thanks to you!Disturb
My pleasure. Thanks for the well-written questions!Hugohugon
Any chance you know how to make this work when the enum contains DescriptionAttributes? When I do that, the value in the setter appears to be a combination of the actual enum value + the DescriptionAttribute which seems to result in an exception because that "value" can't be found in the enum.Disturb
I can't think why that would happen. There has to be something that I don't know about your code. Can you show me the enum declaration (or part of it)? I tried it and the DescriptionAttributes didn't make a difference for me.Hugohugon
Something seems to have gotten corrupted in my project. I noticed that the form would not display properly in the designer anymore. So I basically re-created my project again and now everything is working properly. Thanks for your reply and I appreciate all the help!Disturb
N
1

You need to do something along these lines:

    string[] colors = Enum.GetNames(typeof(colorEnum));
    this.comboBox1.DataSource = colors;

..and to make things easier, just store the favorite color as type string and not as colorEnum

Nonie answered 1/8, 2011 at 1:34 Comment(1)
Thanks, that gets comboBoxFavoriteColor populated with the colors from the color enumeration. But I still need to get the selected color to display in the combobox when I choose a person in comboBoxPeople. I mapped Age to my NumericUpDown control by simply specifying that the Value property of the control should bind to bindingSource1 - Age. That doesn't seem to work for the color combobox, though. I can choose person 1 and set their favorite color to red. But when I change to person 2, the color combobox is still set to red. I want it to update to display the color for that person.Disturb

© 2022 - 2024 — McMap. All rights reserved.