데이터gridview에 디지털 형식 열을 추가합니다

6875 단어 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;

            }

        }

    }

}

좋은 웹페이지 즐겨찾기