public static DataTable BindToEnum(Type enumType)
{
String[] names = System.Enum.GetNames(enumType);
Array values = System.Enum.GetValues(enumType);
DataTable dt = new DataTable();
dt.Columns.Add("key", typeof(string));
dt.Columns.Add("value", typeof(int));
int i = 0;
while (i < names.Length)
{
DataRow dr = dt.NewRow();
dr["key"] = names[i];
dr["value"] = (int)values.GetValue(i);
dt.Rows.Add(dr);
i++;
}
return dt;
}
Here is how you would use this function.
DataTable lo_dt = BindToEnum(typeof(Categories));
ddlCategory.DataSource = lo_dt;
ddlCategory.DataTextField = "key";
ddlCategory.DataValueField = "value";
ddlCategory.DataBind();
No comments:
Post a Comment