DataGridView列中添加CheckBox

来源:互联网 发布:算法导论电子版 编辑:程序博客网 时间:2024/05/01 08:09

 今天工作中遇到需要在DataGridView手动添加CheckBox;根据数据的不同添加的列也不同,除去用户列其他全部添加CheckBox,我还是第一次做个功能,在帮助中找到了相关信息,开始试验遇到一个错误:错误如下:

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事件.

这两个错误找了好久才知道 错在那里 现在分析一下;下面是原帮助代码

private void AddOutOfOfficeColumn(){    DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();    {        column.HeaderText = ColumnName.OutOfOffice.ToString();        column.Name = ColumnName.OutOfOffice.ToString();        column.AutoSizeMode =             DataGridViewAutoSizeColumnMode.DisplayedCells;        column.FlatStyle = FlatStyle.Standard;        column.ThreeState = true;        column.CellTemplate = new DataGridViewCheckBoxCell();        column.CellTemplate.Style.BackColor = Color.Beige;    }    DataGridView1.Columns.Insert(0, column);}

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewcheckboxcolumn(VS.80).aspx这个是DataGridViewCheckBoxColumns类的地址:

请注意红色字地方;如果按照原来的这样就报错;把这两句修改成

column.ThreeState = true;
column.CellTemplate = new DataGridViewCheckBoxCell(true);

或者

column.ThreeState = false;
column.CellTemplate = new DataGridViewCheckBoxCell(false);

就可以了.这就可以放心用了.!!!!!