首页 文章

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

提问于
浏览
1

我有一个数据网格控件绑定到对象的BindingList . 对象类的一个属性是boolean . 我已将datagridview中的该列自定义为复选框类型 .

当datagridview加载具有从绑定列表引入的值的行时,事情正常工作 . 但是,当绘制“新条目”行时,复选框单元格上会抛出System.FormatException .

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

DataGridView中发生以下异常:

System.FormatException: Value '' cannot be converted to type 'Boolean'. at System.Windows.Forms.Formatter.FormatObjects....

我的搜索表明,如果未设置复选框列的true,false和indeterminate值,则会发生这种情况 .
URL引用与我类似的问题:

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/c29427ff-18be-4fb0-a0a7-d1940e1cd817

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

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;

编辑已添加:我尝试过的其他几项内容:

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

  • 尝试使用dataGridView1.Columns [“columnExpedite”] . DefaultCellStyle.NullValue =“false”;结果相同 .

我该如何解决这个异常?

2 回答

  • 1

    我可能读错了,但这听起来像是操作顺序问题 . 我没有看到你将项目添加到列表中的代码所以我不是100%肯定他的,但我猜你要将新对象添加到列表然后修改值

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

    Edit - Added

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

    你能挂钩到DataGridView的DefaultValuesNeeded事件吗?该事件应该在抛出异常之前触发,这应解决问题...

  • 1

    是 . 试试这段代码:

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

相关问题