C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1760字,纯文字阅读大概需要3分钟。
内容图文
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异常全部内容,希望文章能够帮你解决C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。