我在自定义DataGridViewColumn控件中,定义了属性DecimalPlace,在DataGridView的设计界面中增加此控件列没问题,但在修改DecimalPlace属性值点确定后,再打开计界面查询此属性的值却没有改变(在没点确定前,查看此控件的其它属性后再回来看此属性的值是改变的).
运行环境:WindowsXp(SP3)+ Vs.Net2008企业版
麻烦各位帮忙看一下我用了三种方法写了三个控件,都是在点击确定后没有保存最新值,具体代码如下:
一、三个控件代码
1.DataGridViewCustom1
using System;
using System.ComponentModel;
using System.Windows.Forms;public class DataGridViewCustomColumn_1 : DataGridViewColumn
{
    private int m_decimalPlace;
    /// <summary>
    /// 小数位数
    /// </summary>
    [Description("小数位数"), Browsable(true), Category("财务显示单元格")]
    public int DecimalPlace
    {
        get { return m_decimalPlace != 0 ? m_decimalPlace : 2; }
        set
        {
            if (m_decimalPlace != value)
            {
                if (value < 0)
                    throw new Exception("《小数位数》不可以小于 0 !");
                else
                {
                    this.m_decimalPlace = value;
                }
            }
        }
    }
    public DataGridViewCustomColumn_1()
        : base(new DataGridViewCustomCell_1())
    {
    }
}public class DataGridViewCustomCell_1 : DataGridViewTextBoxCell
{
}
2.DataGridViewCustom2
using System;
using System.ComponentModel;
using System.Windows.Forms;public class DataGridViewCustomColumn_2 : DataGridViewColumn
{
    private int m_decimalPlace;
    /// <summary>
    /// 小数位数
    /// </summary>
    [Description("小数位数"), Browsable(true), Category("财务显示单元格")]
    public int DecimalPlace
    {
        get { return m_decimalPlace != 0 ? m_decimalPlace : 2; }
        set
        {
            if (m_decimalPlace != value)
            {
                if (value < 0)
                    throw new Exception("《小数位数》不可以小于 0 !");
                else
                {
                    this.m_decimalPlace = value;
                    DataGridViewCustomCell_2 customCell = (DataGridViewCustomCell_2)this.CellTemplate;
                    customCell.DecimalPlace = value;
                }
            }
        }
    }
    public DataGridViewCustomColumn_2()
        : base(new DataGridViewCustomCell_2())
    {
    }
}public class DataGridViewCustomCell_2 : DataGridViewTextBoxCell
{
    private int m_decimalPlace;
    /// <summary>
    /// 小数位数
    /// </summary>
    public int DecimalPlace
    {
        get { return m_decimalPlace; }
        set
        {
            if (m_decimalPlace != value)
            {
                if (value < 0)
                    throw new Exception("《小数位数》不可以小于 0 !");
                else
                    m_decimalPlace = value;
            }
        }
    }
}
3.DataGridViewCustom3
using System;
using System.ComponentModel;
using System.Windows.Forms;public class DataGridViewCustomColumn_3 : DataGridViewColumn
{
    private int m_decimalPlace;
    /// <summary>
    /// 小数位数
    /// </summary>
    [Description("小数位数"), Browsable(true), Category("财务显示单元格")]
    public int DecimalPlace
    {
        get { return m_decimalPlace != 0 ? m_decimalPlace : 2; }
        set
        {
            if (m_decimalPlace != value)
            {
                if (value < 0)
                    throw new Exception("《小数位数》不可以小于 0 !");
                else
                {
                    this.m_decimalPlace = value;
                    DataGridViewCustomCell_3 customCell = (DataGridViewCustomCell_3)this.CellTemplate;
                    customCell.DecimalPlace = value;                    if (this.DataGridView != null && this.DataGridView.Rows != null)
                    {
                        int rowCount = this.DataGridView.Rows.Count;
                        for (int x = 0; x < rowCount; x++)
                        {
                            foreach (DataGridViewCell dgvCell in this.DataGridView.Rows.SharedRow(x).Cells)
                            {
                                if (dgvCell is DataGridViewCustomCell_3)
                                    (dgvCell as DataGridViewCustomCell_3).DecimalPlace = value;
                            }
                        }
                    }
                }
            }
        }
    }
    public DataGridViewCustomColumn_3()
        : base(new DataGridViewCustomCell_3())
    {
    }
}public class DataGridViewCustomCell_3 : DataGridViewTextBoxCell
{
    private int m_decimalPlace;
    /// <summary>
    /// 小数位数
    /// </summary>
    public int DecimalPlace
    {
        get
        {
            DataGridViewCustomColumn_3 customColumn = OwningColumn as DataGridViewCustomColumn_3;
            if (customColumn != null)
            {
                if (customColumn.DecimalPlace != 0)
                {
                    return customColumn.DecimalPlace;
                }
            }
            return m_decimalPlace;
        }
        set
        {
            if (m_decimalPlace != value)
            {
                if (value < 0)
                    throw new Exception("《小数位数》不可以小于 0 !");
                else
                    m_decimalPlace = value;
            }
        }
    }
}

解决方案 »

  1.   

    二、测试窗口代码
    1.test.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication37
    {
        public partial class test : Form
        {
            public test()
            {
                InitializeComponent();
            }
        }
    }
    2.test.designer.cs
    namespace WindowsApplication37
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column1 = new WindowsApplication37.FinanceTextBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewComboBoxColumn();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column3,
                this.Column1,
                this.Column2});
                dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
                dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
                dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
                dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
                dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
                dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
                this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle2;
                this.dataGridView1.Location = new System.Drawing.Point(58, 62);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(566, 336);
                this.dataGridView1.TabIndex = 0;
                // 
                // Column3
                // 
                this.Column3.HeaderText = "Column3";
                this.Column3.Name = "Column3";
                // 
                // Column1
                // 
                this.Column1.DecimalPlace = 2;
                this.Column1.HeaderText = "Column1";
                this.Column1.LineBold = 0F;
                this.Column1.Name = "Column1";
                this.Column1.NormalColor = System.Drawing.Color.Gray;
                this.Column1.RedColor = System.Drawing.Color.Red;
                this.Column1.SpecialColor = System.Drawing.Color.Blue;
                // 
                // Column2
                // 
                this.Column2.HeaderText = "Column2";
                this.Column2.Name = "Column2";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(664, 504);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
            }
            #endregion        private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
            private FinanceTextBoxColumn Column1;
            private System.Windows.Forms.DataGridViewComboBoxColumn Column2;
        }
    }
      

  2.   

    Sory,test.designer.cs文件传错了.
    namespace WindowsApplication37
    {
        partial class test
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.CustomColumn1 = new DataGridViewCustomColumn_1();
                this.CustomColumn2 = new DataGridViewCustomColumn_2();
                this.CustomColumn3 = new DataGridViewCustomColumn_3();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.CustomColumn1,
                this.CustomColumn2,
                this.CustomColumn3});
                this.dataGridView1.Location = new System.Drawing.Point(68, 26);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(598, 329);
                this.dataGridView1.TabIndex = 0;
                // 
                // CustomColumn1
                // 
                this.CustomColumn1.DecimalPlace = 2;
                this.CustomColumn1.HeaderText = "CustomColumn1";
                this.CustomColumn1.Name = "CustomColumn1";
                // 
                // CustomColumn2
                // 
                this.CustomColumn2.DecimalPlace = 2;
                this.CustomColumn2.HeaderText = "CustomColumn2";
                this.CustomColumn2.Name = "CustomColumn2";
                // 
                // CustomColumn3
                // 
                this.CustomColumn3.DecimalPlace = 2;
                this.CustomColumn3.HeaderText = "CustomColumn3";
                this.CustomColumn3.Name = "CustomColumn3";
                // 
                // test
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(754, 380);
                this.Controls.Add(this.dataGridView1);
                this.Name = "test";
                this.Text = "test";
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.DataGridView dataGridView1;
            private DataGridViewCustomColumn_1 CustomColumn1;
            private DataGridViewCustomColumn_2 CustomColumn2;
            private DataGridViewCustomColumn_3 CustomColumn3;
        }
    }
      

  3.   

    designer.文件是垃圾,不要管,只说出你加三个column的最终实现的目的就可以了
      

  4.   

    DecimalPlace的默认值 为2,当我在设计界面中修改DecimalPlace的值为5并点击确定后,我再次打开设计界面中查看DecimalPlace的值时,发现其值还是2,而不是我之前更改的最新值5
      

  5.   

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;public class DataGridViewCustomColumn_1 : DataGridViewColumn
    {
        public override object Clone()
        {
            DataGridViewCustomColumn_1 col = (DataGridViewCustomColumn_1)base.Clone();
            col.DecimalPlace = this.DecimalPlace;        return col;
        }    private int m_decimalPlace;
        /// <summary>
        /// 小数位数
        /// </summary>
        [Description("小数位数"), Browsable(true), Category("财务显示单元格")]
        public int DecimalPlace
        {
            get
            {
                return m_decimalPlace != 0 ? m_decimalPlace : 2;
            }
            set
            {
                if (m_decimalPlace != value)
                {
                    if (value < 0)
                        throw new Exception("《小数位数》不可以小于 0 !");
                    else
                    {
                        this.m_decimalPlace = value;
                    }
                }
            }
        }
        public DataGridViewCustomColumn_1()
            : base(new DataGridViewCustomCell_1())
        {
        }
    }public class DataGridViewCustomCell_1 : DataGridViewTextBoxCell
    {
    }
      

  6.   

    看了你的代码,感觉你这种需求,没有必要用自定义列,用CellFormatting事件就可以解决
      

  7.   

    新添加的属性要在Clone方法中重新赋值,不然会丢失。
      

  8.   

    sdl2005lyx,我是需要此属性来来确定画线用的颜色