可恶的AutoSizeColumnMode和DataError

解决DataGridViewComboBoxColumn在特定设置下导致DataError的问题。当AutoSizeColumnMode设置为allcells且AutoSizeRowsMode为None时,在表单关闭时可能会出现DataGridViewComboBoxCell值不合法的错误。此问题与DataGridView的数据源先于DataGridView本身被释放有关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

项目中DataGridView中使用了DataGridViewComboBoxColumn,不记得在哪里看到AutoSizeColumnMode设置成allcells,AutoSizeRowsMode=None,觉得蛮好的,结果把所有表单的DGV都设置成这样,不幸的意外发生了:结果关闭表单的时候就会发生DataError错误,说什么DataGridViewComboBoxCell的值不合法。
这时候还不知道哪里出了问题,以为绑定的顺序有问题,结果无奈之中把所有DGV加了个空的DataError 处理。
后来不甘心,到MSDN论坛上一查,原来碰到这个问题的不止我一个:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=157106&SiteID=1&PageID=2
下面是DataGridView Program Manager的解答:
 The issue is that the Form's dispose method is first disposing the datasource that the combobox column is using before it is disposing the DataGridview. This is the same as not databinding the combobox column at all. This only causes a problem when autosizing the rows or autosizing the combobox column since removing the combobox column's datasource causes the row/column to perform autosize layout. As part of the layout, the combobo cell needs to retrieve the FormattedValue, which raises the DataError since the cell cannot find a match in the combobox (since the combobox column's datasource is now null).
和处理办法:
The easiest way to fix this is to change the autosize mode of the grid in the form's dispose method. Alternatively you can manually databind the combo box column, including manually creating the BindingSource. This will allow you to manually dispose the combobox column's binding source on your own -- after the grid has already disposed.
I've opened a bug to address this issue hoping that we can do something in the next version.
提示: 
以后遇到问题多泡泡MSDN的论坛,比国内的好上一百倍,因为有微软的测试或者设计人员及时解答您的提问。

未来:
 Currently we do not have any way to specify any dependency for disposing, but in 2005 we did create better support for dependencies when initializing controls/components. Sounds like we need to create some new support to deal with this type of problem.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值