System.ArgumentException(无效的DataGridViewComboBoxCell值)问题 [英] System.ArgumentException (Invalid DataGridViewComboBoxCell value) Issue

查看:191
本文介绍了System.ArgumentException(无效的DataGridViewComboBoxCell值)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正确地知道你在想什么,只知道谷歌这个话题,并告诉你我已经这样做了......但是所有解决方案导致了同样的问题。



我尝试了以下解决方案,但没有运气(这是网络上的大部分解决方案):

Hi all,

I properly know what you are thinking, just Google the topic, and to let you know I have done so.... But all the solutions lead up to the same problem.

I have tried the following solution, but with no luck (this is most of the solutions on the net):

I was getting an error dialog with the message
"System.Argument.Exception: DataGridViewComboBoxCell value is not valid" when
I had the AutoSizeColumnsMode property for the grid set to "AllCells" but the
problem went away when I set the value to "None". Simply opening the form and
then closing it would cause the error dialog to appear numerous times and the
text in some of the ComboBox cells would disappear.





所以这很奇怪,datagridview在选项卡控件(标签页)中,但是只要从标签页中删除datagridview并将其直接放在表单上,一切正常:confused :: confused:



进一步的调查表明,当datagridview被放置在没有焦点的标签页上时,一个导航到该标签页是发生错误的时间。如果datagridview位于显示窗口时具有焦点的标签页上,则不会出现错误?!?!



[新]

当我将相同的datagridview放在选项卡页面控件之外并直接放在表单上时,整个datagridview控件都可以工作,因此正在选择数据源中的项目。我不相信错误是由数据绑定引起的。

[新]



为什么会发生这种情况?有谁知道如何解决这个问题?



非常感谢提前。

亲切的问候,



So this is the strange thing, the datagridview is with in a tab control (tab page), but as soon as I remove the datagridview from the tab page and place it directly on the form, everything works :confused::confused:

Further investigation show that when the datagridview is placed on a tab page that doesn''t have the focus, and one navigates to that tab page is when the error occurs. If the datagridview is place on a tab page that has focus when the window shown, no error occurs?!?!

[NEW]
When I place the same datagridview outside the tab page control and directly onto the form, the whole datagridview control works, thus an item within the data source is being selected. I do not believe that the error is caused by data binding.
[NEW]

Why is this happening? Does anyone know how to fix this??

Many thanks in advance.
Kind regards,

推荐答案

如果有人还在寻找(就像我一样)...



在我的情况下,这是数据类型不匹配(即使我由于DGV没有受到约束,所以有了int',它仍然将它们视为字符串。我的解决方案是在组合框中的对象和分配期间为默认值分配.ToString()。



If anyone is still looking (like I did)...

In my case, it was data type mismatch (even though I had int''s, it was still treating them as strings) since DGV wasn''t bound. My solution was to assing .ToString() on the objects in the combobox and during the assignment for default values.

if (dgv.Columns["Number"].GetType() == typeof(DataGridViewComboBoxColumn))
{
    DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dgv.Columns["Number"];
    for (int i = 0; i < 100; i++)
        col.Items.Add(i.ToString());

}

row.Cells["Number"].Value = "1";


为了解决这个问题,处理DataError事件datagrid。
for sovle this problem,handle the DataError event datagrid .


您可能已经设置了网格,以便DataGridViewComboBox列与DataSource绑定并尝试设置将值赋予不在DataSource中的值。加载网格时,应确保该列的值设置为DataSource中的值。或者,您可以向绑定到列的DataSource添加值。
You probably have your grid setup so that the DataGridViewComboBox column is bound with a DataSource and are trying to set the value in it to one that is not in the DataSource. When loading the grid you should make sure that the value for that column is set to one that is in your DataSource. Or you could add a value to the DataSource bound to the column.


这篇关于System.ArgumentException(无效的DataGridViewComboBoxCell值)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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