Convert enumerator to datatable for binding purposes

serkan sendur 0 Tallied Votes 2K Views Share

by using the code below you can bind your enums to your dropdown lists


Serkan Şendur

public static DataTable ConvertEnumToDataTable(Enum parEnumToConvert)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            int i = 0;
            foreach (string s in Enum.GetNames(typeof(yourCustomEnum)))
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = i;
                dr["Name"] = s;
                dt.Rows.Add(dr);
                i++;
            }
            return dt;
            
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.