自定义DGV列/单元格的FormattedType问题

时间:2011-05-15 18:38:12

标签: c# datagridview datagridviewcolumn

我创建了一个显示动画图像的自定义DataGridView单元格和列。它非常简单并且工作正常,只是它从DGV抛出一个“DataError”事件: “单元格的格式化值类型错误”

我已从我的两个自定义类中删除了所有代码,但我仍然收到此错误。我已经检查过继承的DGV单元是从它的基础DataGridViewImageCell中提取正确的FormatedValueType。

抛出(并捕获)的异常包含NO stacktrace,因此我无法确定抛出它的方法。

我把一个很小的(< 100行)示例放在一起,说明了这个错误。我希望有人可以为我揭开这一点。我以前创建过其他自定义Cell / Column类型,但它们一直都是基于System.String的,我从来没有抛出这个特定的错误。

以下是代码:

using System;
using System.Windows.Forms;

namespace TestBench
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new BestFormEver());
        }
    }

    public class BestFormEver : Form
    {
        public BestFormEver()
        {
            InitializeComponent();
            dataGridView1.Rows.Add(2);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column1 = new PMD.Library.WinFormControls.Controls.DataGridView.ImageColumn.PMDDataGridViewImageColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1});
            this.dataGridView1.Location = new System.Drawing.Point(25, 24);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(454, 206);
            this.dataGridView1.TabIndex = 0;
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(508, 259);
            this.Controls.Add(this.dataGridView1);
            this.Name = "BestFormEver";
            this.Text = "BestFormEver";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.DataGridView dataGridView1;
        private PMD.Library.WinFormControls.Controls.DataGridView.ImageColumn.PMDDataGridViewImageColumn Column1;
    }

    public class PMDDataGridViewImageColumn : DataGridViewColumn
    {
        public PMDDataGridViewImageColumn() : base(new PMDDataGridViewImageCell())
        {
        }
    }

    class PMDDataGridViewImageCell : DataGridViewImageCell
    {
        public PMDDataGridViewImageCell()
        {
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您需要覆盖PMDDataGridViewImageCell类中的GetFormattedValue()方法,并让它返回一个值(任何值都可以,甚至为null)。

使用您的示例,我添加了一个返回SystemIcons.Question的覆盖 - 现在异常消失了,图标在网格中正确显示。

希望这有帮助。