gpt4 book ai didi

c# - DataGridView 单元格中的自定义控件

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:06 35 4
gpt4 key购买 nike

我在 DataGridView 单元格中有一个自定义控件。它是一个包含复选框项 (CheckBoxComboBox) 的组合框。这是问题:1. 输入 CheckBoxComboBoxes 之一并选择关闭一些复选框项目。 CheckboxComboBox 的文本是选中项目的 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;
}

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

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

最佳答案

想通了。问题是在 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();
}

关于c# - DataGridView 单元格中的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14280960/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com