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
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<T>()
{
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;
}
I gave up on doing this linking the enum directly to the combox. I thought that Koryu had solved it but while it then worked when a user entered data, if I tried to programmatically add a row, with a valid value for the enum, it still gave the error.
Based on my above code, with Koryu's change, I added a row like this.
private void CreateDefaultRows()
{
DataRow Row = DtTbl.NewRow();
Row["Name"] = "FIN";
Row["Engine"] = EngineType.EngineType1;
DtTbl.Rows.Add(Row);
DGV.DataSource = DtTbl;
}
But despite debugging to ensure a valid value I still got the error.
I solved it by using the method of using a datatable. I wrote a generic method in my helper class which returns a datatable for any enum.
public static DataTable Enum2DataTable<T>()
{
DataTable EnumTable = new DataTable();
EnumTable.Columns.Add(new DataColumn("Value", System.Type.GetType("System.Int32")));
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;
}
If I then add the following line when defining the combo box column in my code in the question everything works fine with no other changes and with no errors.
EngineCol.DataSource = Enum2DataTable<EngineType>();
While this works, and I have a reusable method to do this again, it still feels like it should have been possible assigning the enum directly to the combo box as that 'almost' works.
Would love to know why it doesn't work but this solution works at least.
i had this problem myself, here is how i fixed it:
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.ValueType = typeof(MyEnum);
col.ValueMember = "Value";
col.DisplayMember = "Display";
colElementtyp.DataSource = new MyEnum[] { MyEnum.Firstenumvalue, MyEnum.Secondenumvalue }
.Select(value => new { Display = value.ToString(), Value = value })
.ToList();
the DataGridViewComboBoxCell has to be bound to the enum or integer. I guess the column of your datatable is integer, then it should work.
EDIT:
this should work dynamic:
System.Array enumarray = Enum.GetValues(typeof(MyEnum));
List<MyEnum> lst = enumarray.OfType<MyEnum>().ToList();
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.ValueType = typeof(MyEnum);
col.ValueMember = "Value";
col.DisplayMember = "Display";
colElementtyp.DataSource = lst
.Select(value => new { Display = value.ToString(), Value = value })
.ToList();
shorter:
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.ValueType = typeof(MyEnum);
col.ValueMember = "Value";
col.DisplayMember = "Display";
colElementtyp.DataSource = Enum.GetValues(typeof(MyEnum)).OfType<MyEnum>().ToList()
.Select(value => new { Display = value.ToString(), Value = value })
.ToList();