Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loop through an enum and add to dropdownlist

Posted on 2013-10-23
2
Medium Priority
?
315 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

971 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