[英]System.ArgumentException: DataGridViewComboBoxCell value is not valid
dataGridView.Rows.Add(
metaData.Offset.ToString("X2"),
metaData.Length,
metaData.Format, // This parameter goes to a ComboBox cell which throws an
metaData.Description, // exception above
null,
null);
What is the valid way to programmatically assign data to DataGridViewComboBoxCell
?以编程方式将数据分配给DataGridViewComboBoxCell
的有效方法是什么?
To Solve this problem, just add “DataError” for DataGridView.要解决这个问题,只需为 DataGridView 添加“DataError”即可。 That's all, steps: Double click the datagridview and select the “dataerror” event from the event list.就是这样,步骤:双击datagridview并从事件列表中选择“dataerror”事件。
The DataError event enables you to handle exceptions thrown in code that is called by the control during data processing operations. DataError 事件使您能够处理在数据处理操作期间由控件调用的代码中引发的异常。
thats it :)就是这样 :)
To Solve this problem, add event of "DataError" for DataGridView and then要解决此问题,请为 DataGridView 添加“DataError”事件,然后
just write this: e.Cancel = true;只需这样写: e.Cancel = true;
eg:例如:
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
A little late to the party, but I think this can still be usefull to others.聚会有点晚,但我认为这对其他人仍然有用。 In my case I got this error because I added a combobox based on an enum like this:就我而言,我收到此错误是因为我添加了一个基于枚举的组合框,如下所示:
// Combo
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = Enum.GetValues(typeof(PhoneNumberType));
combo.DataPropertyName = "PhoneNumberType";
combo.Name = "PhoneNumberType";
phoneNumberGrid.Columns.Add(combo);
Now when the DataGridView created a new (empty) row, this empty value couldn't be mapped to the combo box.现在,当 DataGridView 创建一个新的(空)行时,这个空值无法映射到组合框。 So instead of ignoring the error, I set the default value for the combo box.因此,我没有忽略错误,而是为组合框设置了默认值。 I added the event DefaultValuesNeeded
and there just set the value to one of the items from the enum.我添加了事件DefaultValuesNeeded
并且只是将值设置为枚举中的项目之一。 Like this:像这样:
private void phoneNumberGrid_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
// Prevent System.ArgumentException: DataGridViewComboBoxCell value is not valid
e.Row.Cells["PhoneNumberType"].Value = PhoneNumberType.Private;
}
.. and the exception went away. .. 异常消失了。
I had a similar issue, where I populated my DataSource with a Dictionary<int,string> myDict
, where I wanted to display the string but have an integer as an underlying value to use:我有一个类似的问题,我用Dictionary<int,string> myDict
填充了我的 DataSource ,我想在其中显示字符串但有一个整数作为要使用的基础值:
comboboxcolumn.DataSource = new BindingSource(myDict, null);
comboboxcolumn.ValueMember = "Key";
comboboxcolumn.DisplayMember = "Value";
what caused my trouble was that I correctly set: comboboxcolumn.ValueType = typeof(int);
导致我遇到麻烦的是我正确设置: comboboxcolumn.ValueType = typeof(int);
but in the CellValueNeeded()
Method, I then set e.Value = someValueOfMyDict;
但是在CellValueNeeded()
方法中,我然后设置e.Value = someValueOfMyDict;
which is a string
.这是一个string
。 Use the int
key instead and you eliminate the error instead of catching it and leaving it unhandled.改用int
键,您可以消除错误,而不是捕获它并对其进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.