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

发布于 2024-11-08 01:49:47 字数 3256 浏览 0 评论 0原文

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

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

抛出(和捕获)的异常不包含堆栈跟踪,因此我无法确定哪个方法抛出它。

我整理了一个很小的(< 100 行)示例来说明该错误。我希望有人能为我阐明这一点。我之前创建过其他自定义单元格/列类型,但它们始终基于 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()
        {
        }
    }
}

I've created a custom DataGridView Cell and Column that displays animated images. It's really simple and works fine except that it's throwing a "DataError" event from the DGV:
"Formatted value of the cell has a wrong type"

I have removed all of the code from my two custom classes and I'm still receiving this error. I've checked that the inherited DGV cell is pulling the correct FormatedValueType from it's base DataGridViewImageCell.

The exception being thrown (and caught) contains NO stacktrace so I can't determine which method is throwing it.

I've put together a tiny (< 100 lines) example that illustrates the error. I'm hoping someone can shed some light on this for me. I've created other custom Cell/Column types before but they have always been System.String based and I've never had this specific error thrown.

Here is the code:

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()
        {
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-11-15 01:49:47

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

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

希望这有帮助。

You need to override the GetFormattedValue() method in your PMDDataGridViewImageCell class and have it return a value (any value will do, even null).

Using your example, I added an override that returns SystemIcons.Question -- the exception is now gone, and the icon is shown properly in the grid.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文