Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Binding Enum to DropDownList.

Binding Enum to DropDownList.

Scheduled Pinned Locked Moved ASP.NET
databasewpfwcf
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DIPAK EMSYS
    wrote on last edited by
    #1

    Hello Guys.... I am trying to Bind DropDownList with enum as follows.

    cmbFromShape.DataSource = Enum.GetValues(typeof(KavirDiam.Common.Shape));
    cmbFromShape.DataBind();

    Following is my enum:

    public enum Shape
    {
    Any,
    Round,
    Princess,
    Emerald,
    Asscher,
    CushionBrilliant,
    CushionModify,
    Pear,
    Radiant,
    SQRadiant,
    Heart,
    Marquise,
    Oval
    }

    While getting Selected Item/Value/Index it is always...returning me 'Any' i.e first member. I am not able to get the value of selected member.

    dipak

    A 1 Reply Last reply
    0
    • D DIPAK EMSYS

      Hello Guys.... I am trying to Bind DropDownList with enum as follows.

      cmbFromShape.DataSource = Enum.GetValues(typeof(KavirDiam.Common.Shape));
      cmbFromShape.DataBind();

      Following is my enum:

      public enum Shape
      {
      Any,
      Round,
      Princess,
      Emerald,
      Asscher,
      CushionBrilliant,
      CushionModify,
      Pear,
      Radiant,
      SQRadiant,
      Heart,
      Marquise,
      Oval
      }

      While getting Selected Item/Value/Index it is always...returning me 'Any' i.e first member. I am not able to get the value of selected member.

      dipak

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      DIPAK@EMSYS wrote:

      While getting Selected Item/Value/Index it is always...returning me 'Any' i.e first member.

      Because you need to give the value as weel for the dropdown list. Try This :

      foreach (Shape shp in Enum.GetValues(typeof(Shape)))
      {
      ListItem Lstitem = new ListItem(Enum.GetName(typeof(Shape), shp), shp.ToString());
      DropDownList1.Items.Add(Lstitem );
      }

      I have found few things over here also. How do you bind an Enum to a DropDownList control in ASP.NET?[^] Thanks !

      Cheers ! Abhijit Jana | MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups