Solved

How can I implement full databinding with an Enum?

Posted on 2008-10-15
2
187 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
2 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
bulid json format 3 46
Icons and Colors for Terms 3 24
In WPF / C# binding a local database in code behind 1 24
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now