Solved

loop through an enum and add to dropdownlist

Posted on 2013-10-23
2
302 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 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

20 Experts available now in Live!

Get 1:1 Help Now