Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用复选框列修复datagrid视图中的FormatException_C#_Datagridview_Checkbox_Formatexception - Fatal编程技术网

C# 使用复选框列修复datagrid视图中的FormatException

C# 使用复选框列修复datagrid视图中的FormatException,c#,datagridview,checkbox,formatexception,C#,Datagridview,Checkbox,Formatexception,我已将datagrid控件绑定到BindingList对象。对象类的属性之一是布尔值。我已经在datagridview中将该列自定义为复选框类型 当datagridview为从绑定列表中引入值的行加载时,一切正常。但是,当绘制“新条目”行时,复选框单元格上会抛出System.FormatException 准确的错误消息(相关部分): DataGridView中发生以下异常: System.FormatException: Value '' cannot be converted to type

我已将datagrid控件绑定到BindingList对象。对象类的属性之一是布尔值。我已经在datagridview中将该列自定义为复选框类型

当datagridview为从绑定列表中引入值的行加载时,一切正常。但是,当绘制“新条目”行时,复选框单元格上会抛出System.FormatException

准确的错误消息(相关部分):

DataGridView中发生以下异常:

System.FormatException: Value '' cannot be converted to type 'Boolean'. at System.Windows.Forms.Formatter.FormatObjects....
我的搜索表明,当未设置复选框列的true、false和Undeterminate值时,可能会发生这种情况。
URL引用了与我类似的问题:

但是,我已经设置了这些值(如下面的代码所示)。除此之外,我找不到与我的问题相关的任何其他信息。我相当肯定这个问题是由于复选框的使用造成的,因为当我将列类型更改为简单的文本框时,没有异常错误,只是一列true/false,其中“newentry”行没有显示任何值

DataGridView代码:

// 
        // dataGridView1
        // 
        this.dataGridView1.AllowUserToResizeRows = false;
        dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
        dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
        dataGridViewCellStyle1.NullValue = null;
        dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
        dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
        this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
        this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
        this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.columnDescription,
        this.columnExpedite,
        this.columnId,
        this.columnQuantity,
        this.columnEntryDate,
        this.columnUpdateDate});
        this.dataGridView1.Location = new System.Drawing.Point(3, 5);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.dataGridView1.Size = new System.Drawing.Size(1015, 241);
        this.dataGridView1.TabIndex = 0;
        // 
        // columnDescription
        // 
        this.columnDescription.DataPropertyName = "Description";
        this.columnDescription.FillWeight = 200F;
        this.columnDescription.HeaderText = "Description";
        this.columnDescription.Name = "columnDescription";
        // 
        // columnExpedite
        // 
        this.columnExpedite.DataPropertyName = "Expedite";
        this.columnExpedite.FalseValue = "false";
        this.columnExpedite.HeaderText = "Expedited";
        this.columnExpedite.Name = "columnExpedite";
        this.columnExpedite.Resizable = System.Windows.Forms.DataGridViewTriState.True;
        this.columnExpedite.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
        this.columnExpedite.TrueValue = "true";
        this.columnExpedite.IndeterminateValue = "false";
        // 
        // columnId
        // 
        this.columnId.DataPropertyName = "Id";
        this.columnId.HeaderText = "Id";
        this.columnId.Name = "columnId";
        this.columnId.Visible = false;
        // 
        // columnQuantity
        // 
        this.columnQuantity.DataPropertyName = "Quantity";
        this.columnQuantity.HeaderText = "Quantity";
        this.columnQuantity.Name = "columnQuantity";
        // 
        // columnEntryDate
        // 
        this.columnEntryDate.DataPropertyName = "EntryDateTime";
        dataGridViewCellStyle2.Format = "g";
        dataGridViewCellStyle2.NullValue = null;
        this.columnEntryDate.DefaultCellStyle = dataGridViewCellStyle2;
        this.columnEntryDate.HeaderText = "Entry Date/Time";
        this.columnEntryDate.Name = "columnEntryDate";
        this.columnEntryDate.ReadOnly = true;
        this.columnEntryDate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
        this.columnEntryDate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
        // 
        // columnUpdateDate
        // 
        this.columnUpdateDate.DataPropertyName = "UpdateDateTime";
        this.columnUpdateDate.HeaderText = "Last Update Date/Time";
        this.columnUpdateDate.Name = "columnUpdateDate";
        this.columnUpdateDate.ReadOnly = true;
        this.columnUpdateDate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
        this.columnUpdateDate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
编辑添加: 我还尝试了另外两件事:

  • 尝试使用DefaultValueRequired事件。当我“触摸”新的输入行时,会加载默认值,但在此之前,当“新输入”行实际正在绘制时,会触发异常

  • 试图使用 dataGridView1.Columns[“ColumnProcess”].DefaultCellStyle.NullValue=“false”; 同样的结果


如何解决此异常?

我可能读错了,但这听起来像是操作顺序问题。我看不到将项目添加到列表中的代码,因此我不能100%确定他是谁,但我猜您是将新对象添加到列表中,然后修改值

您需要确保布尔值不为null。如果要添加新项目,。在将要添加的对象添加到列表之前,需要填充该对象的值。确保这一点的一种可能方法是将默认值设置为false或true,或者在构造函数中设置此值

编辑-添加

我自己没有测试过,但我希望它对你有用

你能钩住DataGridView的事件吗?该事件应在引发异常之前触发,这将解决问题

是的。 请尝试以下代码:

private void dataGridViewEpizode_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        try
        {
            e.Row.Cells[22].Value = false;
        }
        catch (Exception ex)
        {
            mainForm.staticvar.logger.Write(ex);
        }
    }

这是一个很好的观点。我应该解决这个问题,我的bind语句发生在表单加载时。此外,我的构造函数实际上为布尔成员设置了一个默认值。当我单击“新建条目”行时,默认值被拉入(我还设置了默认日期)。问题发生在将“新条目”行绘制到屏幕上时。我尝试了该事件,并设置了一个值。。。它确实会填充,但直到我单击新条目…当新条目行绘制时会触发异常。绘图实际上暂停在该单元格上。