[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I implement full databinding with an Enum?

Posted on 2008-10-15
2
Medium Priority
?
198 Views
Last Modified: 2010-04-15
I can easily load an enum into a combobox, but I am struggling to pass the selected data back to the table.  I am surely missing something simple, so can anyone help?

Chris Bray
public enum Gender
    {
        Unknown,
        Male,
        Female
    }
            
// in the relevant location:
genderAutoCompleteCombo.DataSource = Enum.GetValues(typeof(Gender));
genderAutoCompleteCombo.DisplayMember = "Display";  // this is not right
genderAutoCompleteCombo.ValueMember = "Value";  // nor is this
genderAutoCompleteCombo.DataBindings.Add("SelectedValue", adapter.Table, "GenderId");  // this should work once the other two are fixed!

Open in new window

0
Comment
Question by:chrisbray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 22727620
you need to loop through an enum and create a datatable first and then bind it to the datasource
you cannot do it directly

how to loop through enum and get name and value out - sample given below
int val;
foreach (string str in Enum.GetNames(typeof(enumName)))
{
      val = (int)Enum.Parse(typeof(enumName), str); // this is the int value of the enum
      // str is the name in the enum
      // this you can add to a datable
}
0
 
LVL 3

Author Comment

by:chrisbray
ID: 22729090
Hi ragi0017:

Thaks for that.  I am surprised that there is not a better / quicker way, and I had thought about putting the data in a table in the first place which would also help with localisation.

Howver, I have implemented it as you suggest and put the full working code below in case someone else is looking for it in the future.

Chris Bray

            genderTable = new DataTable();
            DataColumn primaryColumn = new DataColumn("Id", typeof(int));
            genderTable.Columns.Add(primaryColumn);
            genderTable.Columns.Add(new DataColumn("Gender", typeof(string)));
            genderTable.Columns.Add(new DataColumn("Value", typeof(int)));
 
            genderTable.PrimaryKey = new DataColumn[] { primaryColumn };
            
            int val;
            int count = 0;
            foreach (string str in Enum.GetNames(typeof(Gender)))
            {
                val = (int)Enum.Parse(typeof(Gender), str); // this is the int value of the enum
                // str is the name in the enum
                // this you can add to a datable            
 
                DataRow row = genderTable.NewRow();
                row["Id"] = count++;
                row["Gender"] = str;
                row["Value"] = val;
                genderTable.Rows.Add(row);            
            }
 
            genderAutoCompleteCombo.DataSource = genderTable;
            genderAutoCompleteCombo.DisplayMember = "Gender";
            genderAutoCompleteCombo.ValueMember = "Value";
            genderAutoCompleteCombo.DataBindings.Add("SelectedValue", adapter.Table, "GenderId");

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question