Solved

loop through an enum and add to dropdownlist

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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