在DataGridView的单元格的自定义控制 [英] Custom control in DataGridView cell

查看:240
本文介绍了在DataGridView的单元格的自定义控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView单元格的自定义控制。它包含复选框项的组合框(CheckBoxComboBox)。这里的问题: 1.输入CheckBoxComboBoxes之一,选择了一些复选框的项目。该CheckboxComboBox的文本是在检查的项目的CSV字符串。 2.单击不同CheckboxComboBox细胞是EMPTY的(不检查的项目)

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)

结果:新细胞的文本包含旧的单元格的文本。如果我点击一个CheckBoxComboBox单元格,然后一个非CheckBoxComboBox单元格,然后一个CheckBoxComboBox细胞,它工作正常。

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.

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

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

当我通过我的自定义DataGridViewEditingControl调试,看来这EditingControl.Tag没有更新。

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.

东西:

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

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

Any idea how to resolve this issue ? Thanks.

修改1

下面是DataGridViewCheckBoxComboBoxColumn类

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

编辑2 我DataGridViewCheckBoxComboBoxCell覆盖涂料()。当我把一个断点在这种方法中,它被称为两次,当我点击单元格。第一次的调用,FormattedValue的是空的。然而,第二时间,所述FormattedValue的包含previous checkboxcombobox细胞的不正确的字符串。

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?

推荐答案

想通了。问题是,在DetachEditingControl()我需要清除CheckBoxItems。

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

这篇关于在DataGridView的单元格的自定义控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆