Go to Comments





How to use Enum with Combobox in C# WinForms and Asp.Net


Created: 09/04/2011 05:26:12 p.m.




How to use Enum with Combobox in C# WinForms and Asp.Net

In this little tutorial i will try to expline how you can use Enum and fill your Combobox with items from that Enum and make the some thing but in reverse. (Select the item from combobox, check if it belong to your Enum and get proper Enum value)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1) Combobox in WinForms - C#

a) Create your application:



Just add one Combobox and Button

b) InCode behind:

I ahve added Enum type with some countries for testing:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        enum countries
        {
            Germany,
            Argentina,
            United_States,
            Ukraine,
            Russia,
            Peru,
            Colombia,
            Japon
        }

        public Form1()
        {
            InitializeComponent();
        }


c) In form load i gona fill my Combobox with Enum data:


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.ValueMember = "CountrieName";
            comboBox1.DisplayMember = "CountrieDisplay";

            DataTable dt = new DataTable();
            dt.Columns.Add("CountrieName");
            dt.Columns.Add("CountrieDisplay");

            foreach(string countrie in Enum.GetNames(typeof(countries)))
            {
                object[] objects = new object[2];
                objects[0] = countrie;  // This will be a value of the item
                objects[1] = countrie.Replace("_"," "); // This will be the text of the item
                dt.Rows.Add(objects);
            }

            comboBox1.DataSource = dt;
        }

d)  In button click event:

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Enum.IsDefined(typeof(countries), comboBox1.SelectedValue))
            {
                // If selected item value s belong to my enum item then:

                countries myCountrie = (countries)Enum.Parse(typeof(countries), comboBox1.SelectedValue.ToString(), true);
                MessageBox.Show(myCountrie.ToString());

            }
            else
            {
                // If not then:
                MessageBox.Show("Selected item does not belong to the enum list");
            }
        }

    }




Result: how you can see when i click in my button it will show the value property of the item and not the text, if i whant to show the text property just replace this:
comboBox1.SelectedValue.ToString()
by this:
comboBox1.Text.ToString()

Hope this help you!

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2) Combobox in Asp.Net- C#



<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
       
        <br />
       
        <asp:Button ID="Button1" runat="server" Text="Show selected item" onclick="Button1_Click" />
    </div>
    </form>
</body>


a) Code Behind:

public partial class _Default : System.Web.UI.Page
{
    enum countries
    {
        Germany,
        Argentina,
        United_States,
        Ukraine,
        Russia,
        Peru,
        Colombia,
        Japon
    }

b) In Page_Load

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (string countrie in Enum.GetNames(typeof(countries)))
            {
                ListItem item = new ListItem();
                item.Value = countrie;
                item.Text = countrie.Replace("_", " ");
                DropDownList1.Items.Add(item);
            }
        }
    }

c) In Button click event

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Enum.IsDefined(typeof(countries), DropDownList1.SelectedValue))
        {
            countries myCountrie = (countries)Enum.Parse(typeof(countries), DropDownList1.SelectedValue.ToString(), true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Test", "alert('" + myCountrie.ToString() + "');", true);
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Test", "alert('Selected item does not belong to the enum list');", true);
        }
    }
}









Comments: