问题描述
我的 .NET 4.5 WinForms 应用程序中有一个 DataGridView,有两列.一列是常规文本框列,另一列是 DataGridViewComboBoxColumn
,它是一个下拉列表.我试图将列绑定到我的 Filter
对象的 BindingList
并且我希望枚举 FilterType
中的所有内容都显示在落下.这是我的对象和枚举的代码:
I have a DataGridView in my .NET 4.5 WinForms app with two columns. One column is a regular textbox column and the other column is a DataGridViewComboBoxColumn
that is a dropdown. I am trying to bind the columns to a BindingList
of my Filter
object and I would like everything inside of the enum FilterType
to show up in the dropdown. Here's the code for my object and enum:
public class Filter
{
public string keyword { get; set; }
public FilterType type { get; set; }
}
public enum FilterType : int //the strings should appear in the dropdown
{
SESSION = 1,
ORDER = 2,
SHIPMENT = 3
}
我在 VS 2012 设计器中手动创建了列,我将 ColumnType
、HeaderText
和 DataPropertyName
更改为 keyword
和 type
.
使用我找到的答案 here 我已将这两行代码添加到我的表单加载事件中:
I have manually created the columns in the VS 2012 designer where I changed the ColumnType
, HeaderText
, and DataPropertyName
to keyword
and type
.
Using an answer I found here I have added these two lines of code to my form loading event:
colFilterType.DataSource = Enum.GetValues(typeof(FilterType));
colFilterType.ValueType = typeof(FilterType);
当我运行代码时,我最初看到的是一个空白行.每当我点击该行时,无论点击哪一列,我都会收到一个弹出错误.
When I run the code I initially see a blank row. Whenever I click on the row, irregardless of which column is clicked, I get a pop-up error.
System.ArgumentException:DataGridViewComboBoxCell 值无效....请处理DataError事件.
我可以忽略它并在 Keyword
列中输入我想要的任何文本,并且下拉列表神奇地填充了枚举的第一个值.但是,如果我什至将鼠标悬停在下拉菜单上,错误就会再次弹出(请参见屏幕截图).我不确定是什么导致了错误,也不知道在哪里设置断点.我也不知道我是否通过在设计器中制作 DataGridView 的一部分并在代码中修改它们来创建问题.设计者不允许我像在代码中那样设置 DataSource
.它还包含一个我没有在代码中使用过的字段 ValueMember
.
I can ignore it and type whatever text I want into the Keyword
column, and the dropdown is magically filled in with the first value of the enum. However, if I even mouse-over the dropdown the error pops up again (please see the screenshot). I am not sure what causes the error and don't know where to set a breakpoint. I also do not know if I'm creating the problem by making parts of the DataGridView in the designer and modifying those in code. The designer does not let me set the DataSource
the way I did in the code. It also contains a field ValueMember
that I haven't seen used in code.
虽然不理想,但我不介意捕获错误并且不做任何处理,因为下拉菜单似乎会自动填充(并假设所有数据保持完整).
Although not ideal, I wouldn't mind catching the error and doing nothing with it since the dropdown seems to populate itself (and assuming all the data stays intact).
推荐答案
在运行时添加列时,您的代码工作正常.示例代码:
Your code works fine when adding the column at runtime. Sample code:
DataGridViewComboBoxColumn colFilterType = new DataGridViewComboBoxColumn();
colFilterType.HeaderText = "Name you want";
colFilterType.DataSource = Enum.GetValues(typeof(FilterType));
colFilterType.ValueType = typeof(FilterType);
dataGridView1.Columns.Add(colFilterType);
在运行时更改单元格/列的类型时通常会引发您报告的错误,因为每次 DataGridView
以任何方式受到影响时都会触发大量事件:他们中的一些人期望旧类型并使用新类型查找元素(例如,期望一个 int
并找到一个 string
).
Errors like the one you report are usually provoked while changing the type of a cell/column at runtime, because of the big number of events which are triggered every time a DataGridView
is affected in any way: some of them expect the old type and find elements with the new one (e.g., expecting an int
and finding a string
).
这篇关于组合框列上的 DataGridView 默认错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!