C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常
2021/4/9 12:28:41
本文主要是介绍C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常
在DataGridView手动添加了CheckBox列;在窗体Show的时候,遇到一个错误:错误如下:
DataGridView中发生一下异常:System.FormatException:单元格的Formatted值的类型错误.要替换此默认对话框,请处理DataError事件.
点击以后有一对话框错误如下:
DataGridView中发生一下异常:
SystemArgumentException:为DataGridViewCheckBoxCell提供的值的类型错误.
在System.Windows.Forms.DataGridViewCheckBoxCell.set_EditiingCellFormattedValue(Object value)
在System.Windows.Forms.DataGridView.InitializeEditingCellValue (DataGridViewCessStyle&dataGridViewCellStyle,DataGridViewCell&dataGridViewCell)
要替换此默认对话框,请处理DataError事件.
我之前曾经用过CheckBox列,此次和之前的区别是 AllowUserToAddRows=true;我将该属性设置为false,错误没有出现,可以确定该错误与系统自动添加的行及checBox的默认值为null有关。
我在DefaultValuesNeeded事件中增加了默认值,发现错误依旧。
仔细研究发现如下两种解决方法:
其一:在CellFormatting事件中处理
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "AZK") { if (e.Value == null) e.Value = false; }
}
其二:在在CellFormatting事件中不处理,但DataError事件中处理
{ if (dataGridView1.Rows[e.RowIndex].IsNewRow) return; }
Note: DataGridView中几个事件发生的顺序记录如下,以备查寻
在AllowUserToAddRows=true时候的事件发生次序
Form.Show ---> CellFormatting (如果出错,goto DataError),注意这里的NewRow没有触发DefaultValuesNeeded事件。
如果发生其他比如 RowEnter事件
(DefaultValuesNeeded) ---> RowEnter ---> CellClick
(DefaultValuesNeeded) ---> RowsAdded ---> UserAddedRow
DefaultValuesNeeded事件不会发生在 IsNewRow=true的row上
这篇关于C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统