5

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

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

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

当我通过我的自定义 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; 
    }

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

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

4

1 回答 1

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();
    }
于 2013-01-14T21:19:53.167 回答