给datagridview添加一个数字类型列

1.新建一个类:(完整代码)

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;



namespace winclient

{

    class numEdit:NumericUpDown,IDataGridViewEditingControl

    {

        private DataGridView grid;

        private bool valuechanged = false;

        private int rowindex;



        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)

        {

            this.Font = dataGridViewCellStyle.Font;

            this.ForeColor = dataGridViewCellStyle.ForeColor;

            this.BackColor = dataGridViewCellStyle.BackColor;

        }



        public DataGridView EditingControlDataGridView

        {

            get

            {

                return grid;

            }

            set

            {

                grid = value;

            }

        }



        public object EditingControlFormattedValue

        {

            get

            {

                return this.Value;

            }

            set

            {

                if (value != null)

                    this.Value = Convert.ToDecimal(value);

            }

        }



        public int EditingControlRowIndex

        {

            get

            {

                return rowindex;

            }

            set

            {

                rowindex = value;

            }

        }



        public bool EditingControlValueChanged

        {

            get

            {

                return valuechanged;

            }

            set

            {

                valuechanged = value;

            }

        }



        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)

        {

            switch (keyData & Keys.KeyCode)

            {

                case Keys.Left:

                case Keys.Up:

                case Keys.Down:

                case Keys.Right:

                case Keys.Home:

                case Keys.End:

                case Keys.PageDown:

                case Keys.PageUp:

                    return true;

                default:

                    return false;

            }

        }



        public Cursor EditingPanelCursor

        {

            get { return base.Cursor; }

        }



        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)

        {

            return EditingControlFormattedValue;

        }



        public void PrepareEditingControlForEdit(bool selectAll)

        {

            

        }



        public bool RepositionEditingControlOnValueChange

        {

            get { return false; }

        }



        protected override void OnValueChanged(EventArgs eventargs)

        {

            valuechanged = true;

            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);

            base.OnValueChanged(eventargs);

        }

    }

}

 

2.定义单元格:(完整代码)

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;



namespace winclient

{

    class numcell:DataGridViewTextBoxCell

    {

        private numEdit ne;

        public numcell() : base() { }

        private int pointnum;



        public int Pointnum

        {

            get { return pointnum; }

            set 

            {

                if (value < 0)

                {

                    throw new ArgumentOutOfRangeException("输入的数字必须为0以上的整数");

                }

                if (this.pointnum != value)

                {

                    pointnum = value;

                }

            }

        }



        private bool thousands=false;



        public bool Thousands

        {

            get { return thousands; }

            set { thousands = value; }

        }

        

        private decimal max=10000000;

        

        public decimal Max

        {

            get { return max; }

            set

            {

                if (value <= 0)

                {

                    throw new ArgumentOutOfRangeException("输入的数字必须为大于0的整数");

                }

                if (this.max != value)

                {

                    max = value;

                }

            }

        }



        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)

        {

            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            ne = DataGridView.EditingControl as numEdit;

            ne.Value = Convert.ToDecimal(this.Value);

            ne.DecimalPlaces = pointnum;

            if (ne != null)

            {

                ne.Size = this.Size;

                ne.DecimalPlaces = pointnum;

                ne.Maximum = max;

                ne.ThousandsSeparator = thousands;

            }

        }



        public override Type EditType

        {

            get

            {

                return typeof(numEdit);

            }

        }



        public override Type ValueType

        {

            get

            {

                Type valueType = base.ValueType;

                if (valueType != null)

                {

                    return valueType;

                }

                return ValueType;

            }

        }



        protected override object GetValue(int rowIndex)

        {

            return base.GetValue(rowIndex);

        }



        public override object DefaultNewRowValue

        {

            get

            {

                return decimal.One;

            }

        }



        public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.ComponentModel.TypeConverter valueTypeConverter)

        {

            return base.ParseFormattedValue(formattedValue.ToString(), cellStyle, formattedValueTypeConverter, valueTypeConverter);

        }

    }

}

 

3.定义列:(完整代码)

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;



namespace winclient

{

    class numcolumn:DataGridViewColumn

    {

        public numcolumn() : base(new numcell()) { }



        public override DataGridViewCell CellTemplate

        {

            get

            {

                return base.CellTemplate;

            }

            set

            {

                if (value != null && !value.GetType().IsAssignableFrom(typeof(numcell)))

                    throw new InvalidCastException("列中只能使用数字单元格");

                base.CellTemplate = value;

            }

        }

    }

}

你可能感兴趣的:(datagridview)