Sunday, April 19, 2009

Enum Databinding to DropDownList in C#

//Enumerator
public enum Month
{
Jan = 1,
Feb = 2,
Mar = 3,
Apr = 4,
May = 5,
Jun = 6,
Jul = 7,
Aug = 8,
Sep = 9,
Oct = 10,
Nov = 11,
Dec = 12
}

//Databinding enum text values
ddlMonth.DataSource = Enum.GetNames(typeof(Month));

//Assigning enum text value to a databound object
ddlMonth.SelectedValue = ((Month)object.Date.Value.Month).ToString();

No comments: