C# 从 DataGridView 中的枚举创建下拉列表选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create drop down list options from enum in a DataGridView
提问by Timothy Carter
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 来创建此类的集合。这个类的大部分属性都是字符串。但是,我希望用户能够设置的属性之一是枚举。因此,我希望用户界面具有此枚举的下拉列表,以限制用户输入无效值。目前,我正在获取对象的初始列表,将它们添加到 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 作为我的数据源,这只是我半工作的选项。
采纳答案by Ozgur Ozcitak
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);
回答by Ozgur Ozcitak
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 对象,而将 BindingSource 对象的 DataSource 设置为 a BindingList<Your Class>
,您可以通过将 an 传递IList
给构造函数来填充该对象,而不是使用 DataTable 。
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 更可取(即它是从数据库调用返回的)。