DataGridView 单元格中的自定义控件

Web*_*Web 5 .net c# datagridview

我在 DataGridView 单元格中有一个自定义控件。它是一个包含复选框项目(CheckBoxComboBox)的组合框。问题如下: 1. 输入 CheckBoxComboBoxes 之一并选择关闭一些复选框项目。CheckboxComboBox 的 Text 是选中项的 csv 字符串。2. 单击一个不同的 CheckboxComboBox 单元格是空的(没有选中的项目)

结果:新单元格的文本包含旧单元格的文本。如果我单击 CheckBoxComboBox 单元格,然后单击非 CheckBoxComboBox 单元格,然后单击 CheckBoxComboBox 单元格,则它可以正常工作。

我已阅读并实现了基于此文档的自定义 DataGridViewCell: How to: Host Controls in Windows Forms DataGridView Cells

当我通过我的自定义 DataGridViewEditingControl 进行调试时,EditingControl.Tag 似乎没有更新。

所以我假设我在重用 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; 
    }
Run Code Online (Sandbox Code Playgroud)

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();
    }
Run Code Online (Sandbox Code Playgroud)

知道如何解决这个问题吗?谢谢。

编辑 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 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)
    {}
Run Code Online (Sandbox Code Playgroud)

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

Web*_*Web 2

弄清楚了。问题是在 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();
    }
Run Code Online (Sandbox Code Playgroud)