Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create drop down list options from enum in a DataGridView

I currently have a class and I'm trying to create an easy GUI to create a collection of this class. Most of the attributes of this class are strings. However, one of the attributes I want the user to be able to set is an Enum. Therefore, I would like the user interface, to have a dropdownlist for this enum, to restrict the user from entering a value that is not valid. Currently, I am taking the initial list of objects, adding them to a DataTable and setting the DataSource of my DataGridView to the table. Works nicely, even creates a checkbox column for the one Boolean property. But, I don't know how to make the column for the enum into a dropdownlist. I am using C# and .NET 2.0.

Also, I have tried assigning the DataSource of the DataGridView to the list of my objects, but when I do this, it doesn't help with the enum and I'm unable to create new rows in the DataGridView, but I am definitely not bound to using a DataTable as my DataSource, it was simply the option I have semi-working.

like image 487
Timothy Carter Avatar asked Sep 11 '08 12:09

Timothy Carter


1 Answers

I do not know if that would work with a DataGridView column but it works with ComboBoxes:

comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));

and:

MyEnum value = (MyEnum)comboBox1.SelectedValue;

UPDATE: It works with DataGridView columns too, just remember to set the value type.

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.Name = "My Enum Column";
col.DataSource = Enum.GetValues(typeof(MyEnum));
col.ValueType = typeof(MyEnum);
dataGridView1.Columns.Add(col);
like image 189
Ozgur Ozcitak Avatar answered Oct 19 '22 16:10

Ozgur Ozcitak