DataGridView单元格中的自定义控件

本文关键字:自定义控件 单元格 DataGridView | 更新日期: 2023-09-27 17:51:17

我在DataGridView单元格中有一个自定义控件。它是一个包含复选框项的组合框(CheckBoxComboBox)。问题是:1. 输入其中一个CheckBoxComboBoxes并选择一些复选框项。CheckboxComboBox的文本是复选项的csv字符串。2. 单击另一个空的CheckboxComboBox单元格(没有选中的项)

Result:新单元格的文本包含旧单元格的文本。如果我点击CheckBoxComboBox单元格,然后是非CheckBoxComboBox单元格,然后是CheckBoxComboBox单元格,它会正确工作。

我已经阅读并实现了基于本文档的自定义DataGridViewCell:如何:Windows窗体中的主机控件DataGridView单元格

当我调试通过我的自定义DataGridViewEditingControl,它似乎是编辑控制。标签未更新

所以我假设我有EditingControl被重用的问题。

我尝试过的事情:

1。覆盖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。覆盖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();
    }

你知道怎么解决这个问题吗?谢谢。

编辑1

这是DataGridViewCheckBoxComboBoxColumn类

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();
        }
    }
}

编辑2 我的DataGridViewCheckBoxComboBoxCell覆盖了Paint()。当我在这个方法上放置一个断点时,当我单击单元格时,它会被调用两次。第一次调用时,formattedValue为空。但是,第二次,formattedValue包含前一个checkboxcombobox单元格的错误字符串。

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)
    {}

为什么它被调用两次,为什么在第二次调用时它包含正确单元格的错误格式值?

DataGridView单元格中的自定义控件

明白了。问题是,在DetachEditingControl()中,我需要清除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();
    }