我有一个绑定到对象 BindingList 的数据网格控件。对象类的属性之一是布尔值。我已将 datagridview 中的该列自定义为复选框类型。
当 datagridview 为具有从绑定列表中引入的值的行加载时,一切正常。但是,当绘制“新条目”行时,会在复选框单元格上引发 System.FormatException。
确切的错误消息(相关部分):
DataGridView 出现以下异常:
System.FormatException: Value '' cannot be converted to type 'Boolean'. at System.Windows.Forms.Formatter.FormatObjects....
我的搜索表明,当未设置复选框列的真、假和不确定值时,可能会发生这种情况。
URL 引用与我类似的问题:
但是,我已经设置了这些值(如下面的代码所示)。除此之外,我找不到与我的问题相关的任何其他信息。我相当确定问题局限于复选框的使用,因为当我将列类型更改为简单的文本框时,我没有收到异常错误,只是一列 true / false 显示“新条目”行没有价值。
数据网格视图代码:
//
// 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;
编辑添加: 我尝试过的其他几件事:
尝试使用 DefaultValueNeeded 事件。当我“触摸”新条目行时加载默认值,但在此之前触发异常,当“新条目”行实际绘制时。
尝试使用 dataGridView1.Columns["columnExpedite"].DefaultCellStyle.NullValue = "false"; 结果相同。
我该如何解决这个异常?