Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom control in DataGridView cell

I have a custom control in a DataGridView cell. Its a combobox that contains checkbox items (CheckBoxComboBox). Here's the issue : 1. Enter one of the CheckBoxComboBoxes and select off some checkbox items. The Text of the CheckboxComboBox is a csv string of the checked items. 2. Click a different CheckboxComboBox cell that is emtpy (no checked items)

Result : The text of the new cell contains the text of the old cell. If I click a CheckBoxComboBox cell, then a non-CheckBoxComboBox cell and then a CheckBoxComboBox cell, it works correctly.

I have read and implemented the custom DataGridViewCell based on this document : How to: Host Controls in Windows Forms DataGridView Cells

When I debug through my custom DataGridViewEditingControl, it appears that the EditingControl.Tag isn't updated.

So I am assuming that I have an issue with the EditingControl is being reused.

Things I have tried :

1. Override DataGridViewCell.Clone()

    public override object Clone()
    {
        DataGridViewCheckBoxComboBoxCell checkBoxComboBoxCell = base.Clone() as DataGridViewCheckBoxComboBoxCell;
        if (checkBoxComboBoxCell != null)
        {
            checkBoxComboBoxCell.Tag = this.Tag;
            checkBoxComboBoxCell.Values = this.Values;


        }
        return checkBoxComboBoxCell; 
    }

2. Override DataGridViewCell.DetachEditingControl()

public override void DetachEditingControl()
    {
        DataGridView dataGridView = this.DataGridView;

        if (dataGridView == null || dataGridView.EditingControl == null)
        {
            throw new InvalidOperationException("Cell is detached or its grid has no editing control.");
        }

        DataGridViewCheckBoxComboBoxCellEditingControl ctl = DataGridView.EditingControl as DataGridViewCheckBoxComboBoxCellEditingControl;
        if (ctl != null)
        {
            //Just trying different things
            ctl.EditingControlFormattedValue = String.Empty;
            ctl.Text = string.Empty;
            ctl.Tag = null;
            ctl.Items.Clear();
        }

        base.DetachEditingControl();
    }

Any idea how to resolve this issue ? Thanks.

EDIT 1

Here is the DataGridViewCheckBoxComboBoxColumn class

class DataGridViewCheckBoxComboBoxColumn : DataGridViewColumn
{
     public override object Clone()
    {
        DataGridViewCheckBoxComboBoxColumn that = (DataGridViewCheckBoxComboBoxColumn)base.Clone();

        return that;
    }

    private DataGridViewCheckBoxComboBoxCell _cell = null;
    public DataGridViewCheckBoxComboBoxColumn()
    {
        _cell = new DataGridViewCheckBoxComboBoxCell();
        base.CellTemplate = _cell;
    }

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

    public string Values
    {
        set
        {
            _cell.Tag = value;
            this.Tag = value;
        }
        get
        {
            return _cell.Tag.ToString();
        }
    }
}

EDIT 2 My DataGridViewCheckBoxComboBoxCell overrides Paint(). When I put a breakpoint on this method, it gets called twice when I click the cell. The first time its called, the formattedValue is empty. However, the second time, the formattedValue contains the incorrect string of the previous checkboxcombobox cell.

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                                  DataGridViewElementStates cellState, object value, object formattedValue,
                                  string errorText, DataGridViewCellStyle cellStyle,
                                  DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                  DataGridViewPaintParts paintParts)
    {}

Why does it get called twice and why on the second call does it contain the wrong formatted value for the correct cell?

like image 535
Web Avatar asked Nov 03 '22 06:11

Web


1 Answers

Figured it out. The issue was that in the DetachEditingControl() I needed to clear the CheckBoxItems.

public override void DetachEditingControl()
    {
        DataGridView dataGridView = this.DataGridView;

        if (dataGridView == null || dataGridView.EditingControl == null)
        {
            throw new InvalidOperationException("Cell is detached or its grid has no editing control.");
        }

        DataGridViewCheckBoxComboBoxCellEditingControl ctl = DataGridView.EditingControl as DataGridViewCheckBoxComboBoxCellEditingControl;
        if (ctl != null)
        {
            ctl.CheckBoxItems.Clear();  //Forgot to do this.
            ctl.EditingControlFormattedValue = String.Empty;
        }

        base.DetachEditingControl();
    }
like image 117
Web Avatar answered Nov 12 '22 11:11

Web