DataGridView linked to DataTable with Combobox column based on enum

后端 未结 3 1391
陌清茗
陌清茗 2020-12-19 17:22

Having spent a lot of yesterday searching on this one I have to give up.

I have a DataGridView linked to a Datatable. One of the columns is an enum and I want that t

3条回答
  •  悲&欢浪女
    2020-12-19 17:50

    Using RosieC's code I still get the exception when the underlying type of an enum is not an Int32. This can be fixed with a minor modification:

    public static DataTable Enum2DataTable()
    {
        DataTable EnumTable = new DataTable();
        EnumTable.Columns.Add(new DataColumn("Value", Enum.GetUnderlyingType(typeof(T))));
        EnumTable.Columns.Add(new DataColumn("Display", System.Type.GetType("System.String")));
        DataRow EnumRow;
        foreach (T E in Enum.GetValues(typeof(T)))
        {
            EnumRow = EnumTable.NewRow();
            EnumRow["Value"] = E;
            EnumRow["Display"] = E.ToString();
            EnumTable.Rows.Add(EnumRow);
        }
    
        return EnumTable;
    }
    

提交回复
热议问题