在datagridview中托管自定义复选框

时间:2019-04-08 06:17:23

标签: c# winforms datagridview

我有一个带复选框的datagridview,我从提供材质复选框的NuGet包下载了Material Skin,我想在datagridview中使用此材质检查。

实质性皮肤NuGet软件包的链接:-https://www.nuget.org/packages/MaterialSkin/

我尝试过,但是它给出了一个例外:

  

System.FormatException :单元格的格式化值类型错误。要替换此默认对话框,请处理数据错误事件

 class ChackBoxColuman : DataGridViewColumn
    {

        public ChackBoxColuman() : base(new ChackBoxCell())
        {
        }



        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(ChackBoxCell)))
                {
                    throw new InvalidCastException("Must be a ChackBoxCedll");
                }
                base.CellTemplate = value;
            }
        }

    }

    class ChackBoxCell : DataGridViewCheckBoxCell {

        public ChackBoxCell() : base() {
            this.Style.Format = "";
        }



        public override object DefaultNewRowValue
        {
            get
            {
                return false;
            }
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue,dataGridViewCellStyle);
            ChackBoxEdit ctr = DataGridView.EditingControl as ChackBoxEdit;
            if (this.Value == null)
            {
                ctr.Checked = false;
            }
            else {

                ctr.Checked = System.Convert.ToBoolean(this.Value);
            }
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing control that CalendarCell uses.
                return typeof(ChackBoxEdit);
            }
        }


        public override Type ValueType
        {
            get
            {
                return typeof(bool);
            }

        }


    }



    class ChackBoxEdit : MaterialChackbox,IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

       public ChackBoxEdit():base() {

        }

        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        public int EditingControlRowIndex {

            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }

        }
        public bool EditingControlValueChanged {

            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
               }

        public Cursor EditingPanelCursor {

            get
            {
                return base.Cursor;
            }
        }




        public bool RepositionEditingControlOnValueChange {

            get
            {
                return false;
            }
        }

        public object EditingControlFormattedValue
        {
            get
            {
                return this.Checked;
            }
            set
            {
                this.Checked =Convert.ToBoolean(value);
            }
        }


        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
        }

        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.
            switch (key & 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 !dataGridViewWantsInputKey;
            }
        }
        //Get
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return this.Checked;
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
           // throw new NotImplementedException();
        }


        //

        protected override void OnCheckedChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell
            // have changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnCheckedChanged(eventargs);
        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.



    }

0 个答案:

没有答案