简体   繁体   中英

Data binding between enum to ComboBox

Public Class Person
{
 private enum accountType
 {
   Savings,
   Cheking
 },

}

In Windows form I have a comboBox Account Type. How can i bind data from Person Class to Windows form combobox. When I run the form combobox will show the enum list automatically. How can i solve it. Anybody help me. One portion enum accountType will be public. I am new in C#.

Hei i hope this could help you

      public enum AccountType
      {
         Savings,
         Cheking
      }

Write the following code in your Form1.cs file I am assuming your Form name is Form1

    public Form1 ()
    {
        InitializeComponent();
        BindComboList();
    }

    private void BindComboList()
    {
        var values = Enum.GetValues(typeof(AccountType));
        foreach (var item in values)
        {
            cmbAccountType.Items.Add(item);
        }
    }

You are done.

Try this;

cbaccountType.DataSource=Enum.GetValues(typeof(accountType));

where cbaccountType is you ComboBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM