Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:30:44+00:00 2026-05-24T04:30:44+00:00

I created the following class to model a person: namespace DataBindingTest { public enum

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:30:45+00:00Added an answer on May 24, 2026 at 4:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Model: public class Person { public string Name {get;set;} public
I created an assembly (dll) with the following classes: Person.cs [Table(People)] public class Person
I have the following model: public class Person { public string Name { get;
In an assembly I created a class like the following: [DataObject(true)] public class A
I have a company model and a person model with the following relationships: class
I have created the following M2M table with an intermediary model -- class Position(models.Model):
I have a following model with mongoid rails3 class Address include Mongoid::Document embedded_in :person,
In the following model: class header(models.Model): title = models.CharField(max_length = 255) created_by = models.CharField(max_length
I have an ExpenseType object that I have created with the following migration: class
I have created UITableCellView class called NoteCell . The header defines the following: #import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.