The
DataTextField is the "Text" that is displayed to the user.
The
DataValueField is the selection value associated with the displayed text.
Here example is binding enum values to the dropdown.
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, int> CustomerTypeList = new Dictionary<string, int>();
FieldInfo fi;
DescriptionAttribute da;
foreach (colors enumValue in Enum.GetValues(typeof(colors)))
{
fi = typeof(colors).GetField((enumValue.ToString()));
da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,typeof(DescriptionAttribute));
if (da != null)
{
CustomerTypeList.Add(da.Description, (int)enumValue);
}
}
DropDownList1.DataSource = CustomerTypeList;
DropDownList1.DataTextField = "Key";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
}
public enum colors
{
[DescriptionAttribute("Blue")]
Blue = 0,
[DescriptionAttribute("Green")]
Green = 1,
[DescriptionAttribute("Voilet")]
Voilet = 2,
[DescriptionAttribute("BabyPink")]
BabyPink=4,
[DescriptionAttribute("Purple")]
Purple=3
}
Output:
Items in drop down will be,
Blue
Green
Voilet
Purple
BabyPink
Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator