Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

loop through an enum and add to dropdownlist

Posted on 2013-10-23
2
Medium Priority
?
312 Views
Last Modified: 2013-10-24
I need to loop through an enum with the values for Gender that need to be added to a dropdownlist. How do I set up my foreach statement for this?

Admin.SiteCentralLookupFactoryLookUpTypes LookUpGenders = Admin.SiteCentralLookupFactoryLookUpTypes.Genders;

Where Admin.SiteCentralLookupFactoryLookUpTypes is an enum

LookupValues = MyFactory.GetLookupItems(LookUpGenders, AssociateID);
where (Local Variable) List<Admin.LookUpObject> LookupValues

[0]
LookUpDescription = "Female"
LookUpDescriptionField = "Female"

LookUpId = "1"
LookUpIdField = "1"

LookUpName = "F"
LookUpNameField = "F"

[1]
LookUpDescription = "Male"
LookUpDescriptionField = "Male"

LookUpId = "2"
LookUpIdField = "2"

LookUpName = "M"
LookUpNameField = "M"

            
//GENDER ID DROPDOWNLIST
LookupValues = MyFactory.GetLookupItems(LookUpGenders, EmployeeID);
foreach (LookupValues eGenders in Enum.GetValues(typeof(LookupValues)))
{
      ddlGenderID.Items.Add(eGenders);        
}

ddlGenderID.SelectedValue = EmployeeDetails.GenderID.ToString();
0
Comment
Question by:RedXavier2k4
[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 16

Accepted Solution

by:
Easwaran Paramasivam earned 2000 total points
ID: 39595133
Try like given below:
enum Gender
{
    Female = 1,
    Male = 2
}


Array itemValues = System.Enum.GetValues(typeof(Gender));
Array itemNames = System.Enum.GetNames(typeof(Gender));

for (int i = 0; i <= itemNames.Length - 1 ; i++) 
{
    ListItem item = new ListItem(itemNames[i], itemValues[i]);
    ddlGenderID.Items.Add(item);
}

Open in new window

0
 

Author Closing Comment

by:RedXavier2k4
ID: 39596825
Thanks That's what I was looking for. I also found a way with using the datasource.

                //GENDER ID DROPDOWNLIST
                LookupValues = MyFactory.GetLookupItems(Admin.SiteCentralLookupFactoryLookUpTypes.Genders, tSiteID);
                ddlGenderID.DataSource = LookupValues;
                ddlGenderID.DataTextField = "LookupName";
                ddlGenderID.DataValueField = "LookupId";
                ddlGenderID.DataBind();
                if (EmployeeDetails.GenderID.ToString().Trim() != "")
                {
                    ddlGenderID.SelectedValue = EmployeeDetails.GenderID.ToString();
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

660 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