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.
我目前有一个类,我正在尝试创建一个简单的GUI来创建这个类的集合。这个类的大多数属性都是字符串。但是,我希望用户能够设置的属性之一是Enum。因此,我希望用户界面具有此枚举的下拉列表,以限制用户输入无效的值。目前,我正在获取对象的初始列表,将它们添加到DataTable并将DataGridView的DataSource设置为表。很好地工作,甚至为一个布尔属性创建一个复选框列。但是,我不知道如何将枚举列放入下拉列表中。我正在使用C#和.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.
此外,我已经尝试将DataGridView的DataSource分配给我的对象列表,但是当我这样做时,它对枚举没有帮助,我无法在DataGridView中创建新行,但我绝对不是绑定使用DataTable作为我的DataSource,它只是我半工作的选项。
2 个解决方案
#1
36
I do not know if that would work with a DataGridView column but it works with ComboBoxes:
我不知道这是否适用于DataGridView列,但它适用于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.
更新:它也适用于DataGridView列,只记得设置值类型。
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.Name = "My Enum Column";
col.DataSource = Enum.GetValues(typeof(MyEnum));
col.ValueType = typeof(MyEnum);
dataGridView1.Columns.Add(col);
#2
3
Or, if you need to do some filtering of the enumerator values, you can loop through Enum.GetValues(typeof(EnumeratorName)) and add the ones you want using:
或者,如果您需要对枚举器值进行一些过滤,则可以循环遍历Enum.GetValues(typeof(EnumeratorName))并添加您想要的那些:
dataGridViewComboBoxColumn.Items.Add(EnumeratorValue)
As an aside, rather than using a DataTable, you can set the DataSource of the DataGridView to a BindingSource object, with the DataSource of the BindingSource object set to a BindingList<Your Class>, which you populate by passing an IList into the constructor.
另外,您可以将DataGridView的DataSource设置为BindingSource对象,而不是使用DataTable,将BindingSource对象的DataSource设置为BindingList
Actually, I'd be interested to know from anyone if this is preferable to using a DataTable in situations where you don't already have one (i.e. it is returned from a database call).
实际上,我有兴趣知道在没有数据表的情况下使用DataTable是否比使用DataTable更好(即从数据库调用返回)。
#1
36
I do not know if that would work with a DataGridView column but it works with ComboBoxes:
我不知道这是否适用于DataGridView列,但它适用于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.
更新:它也适用于DataGridView列,只记得设置值类型。
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.Name = "My Enum Column";
col.DataSource = Enum.GetValues(typeof(MyEnum));
col.ValueType = typeof(MyEnum);
dataGridView1.Columns.Add(col);
#2
3
Or, if you need to do some filtering of the enumerator values, you can loop through Enum.GetValues(typeof(EnumeratorName)) and add the ones you want using:
或者,如果您需要对枚举器值进行一些过滤,则可以循环遍历Enum.GetValues(typeof(EnumeratorName))并添加您想要的那些:
dataGridViewComboBoxColumn.Items.Add(EnumeratorValue)
As an aside, rather than using a DataTable, you can set the DataSource of the DataGridView to a BindingSource object, with the DataSource of the BindingSource object set to a BindingList<Your Class>, which you populate by passing an IList into the constructor.
另外,您可以将DataGridView的DataSource设置为BindingSource对象,而不是使用DataTable,将BindingSource对象的DataSource设置为BindingList
Actually, I'd be interested to know from anyone if this is preferable to using a DataTable in situations where you don't already have one (i.e. it is returned from a database call).
实际上,我有兴趣知道在没有数据表的情况下使用DataTable是否比使用DataTable更好(即从数据库调用返回)。