ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常

2021-04-09 09:35:13  阅读:235  来源: 互联网

标签:CheckBox 错误 C# System DataGridView --- DefaultValuesNeeded 事件


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上

标签:CheckBox,错误,C#,System,DataGridView,---,DefaultValuesNeeded,事件
来源: https://www.cnblogs.com/guanshan/p/guan2021-4-9_001.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有