Binding Enum to DropDownList.
-
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
-
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
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.