none
how to host custom checkbox in datagridview RRS feed

  • Question

  • I have a datagridview with checkbox and I downloaded Material Skin from the NuGet package that provides material checkbox I want to use this material check inside my datagridview.


    I tried but it's giving an exception:

    System.FormatException: Formatted value of the cell has <g class="gr_ gr_17 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="17" id="17">a wrong</g> type. To replace this default dialog please handle the data error event

    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.
                    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.
    
    
    
        }



    Tuesday, April 9, 2019 11:08 AM

All replies

  • Hi jay721,

    "Material Skin" seems to be a third-party dll, and here only discuss issues related to Microsoft products. Maybe you can go to the relevant Github: IgnaceMaes/MaterialSkin to submit the issue.

    You can also try to use the DataGridViewCheckBoxColumn provided in the DataGridView. Here is a simple demo:

        DataGridViewCheckBoxColumn cbc = new DataGridViewCheckBoxColumn
        {
            HeaderText = "Include A",
            FalseValue = "0",
            TrueValue = "1"
        };
        dataGridView1.Columns.Insert(0, cbc);

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 10, 2019 1:52 AM
  • Thank you for your reply, I put my point in Github: Ignace <g class="gr_ gr_64 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="64" id="64">Mars ,</g> when I find the solution of this point I submit will do.
    Thursday, April 11, 2019 5:11 AM