DataGridView ComboBox列上的默认错误 [英] DataGridView Default Error on ComboBox Column

查看:345
本文介绍了DataGridView ComboBox列上的默认错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView在我的.NET 4.5 WinForms应用程序与两列。一列是一个常规的文本框列,另一列是一个 DataGridViewComboBoxColumn ,它是一个下拉列表。我试图将列绑定到我的 Filter 对象的 BindingList ,我想让枚举中的所有内容 FilterType 显示在下拉列表中。以下是我的对象和枚举的代码:

  public class Filter 
{
public string keyword {get ;组; }
public FilterType type {get;组; }
}

public enum FilterType:int //字符串应该出现在下拉列表
{
SESSION = 1,
ORDER = 2,
SHIPMENT = 3
}

我已手动创建了VS 2012中的列设计师改变了 ColumnType HeaderText DataPropertyName 关键字类型

使用答案我发现 here 我已经在表单加载事件中添加了这两行代码:

  colFilterType.DataSource = Enum.GetValues(typeof(FilterType)); 
colFilterType.ValueType = typeof(FilterType);

当我运行代码时,我最初看到一个空行。每当我点击该行时,无论单击哪一列,我都会出现一个弹出错误。


System.ArgumentException:DataGridViewComboBoxCell值无效。 ...请处理DataError事件。


我可以忽略它,并将所需的任何文本输入到关键字列,下拉列表以令牌神奇地填充了枚举的第一个值。但是,如果我甚至鼠标悬停在下拉列表上,则会再次弹出错误(请参阅屏幕截图)。我不知道是什么导致错误,不知道在哪里设置断点。我也不知道我是否通过在设计器中制作DataGridView的部分并修改代码来创建问题。设计师不让我按照我在代码中的方式设置 DataSource 。它还包含我在代码中没有看到的字段 ValueMember





虽然不太理想,但我不介意捕捉错误,并且不做任何事情,因为下拉列表似乎填充了自己(并假定所有数据保持不变)。

解决方案

在运行时添加列时,您的代码工作正常。示例代码:

  DataGridViewComboBoxColumn colFilterType = new DataGridViewComboBoxColumn(); 
colFilterType.HeaderText =你想要的名字;
colFilterType.DataSource = Enum.GetValues(typeof(FilterType));
colFilterType.ValueType = typeof(FilterType);
dataGridView1.Columns.Add(colFilterType);

像您报告的错误一样,在运行时更改单元格/列的类型时会引发错误,因为每次 DataGridView 以任何方式受到影响时触发的大量事件:其中一些事件期望旧类型并使用新的元素(例如,期待一个 int 并找到一个字符串)。


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
}

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 value is not valid. ... please handle the DataError event.

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);

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 ComboBox列上的默认错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆