开发者

FormattedType issue with custom DGV Column/Cell

开发者 https://www.devze.com 2023-03-05 21:22 出处:网络
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:

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 t开发者_StackOverflow中文版hat 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()
        {
        }
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号