我在DataGridView单元格中有一个自定义控件。它是一个包含复选框项目(CheckBoxComboBox)的组合框。问题如下: 1.输入其中一个CheckBoxComboBox并选择一些复选框项目。 CheckboxComboBox的文本是检查项目的csv字符串。 2.单击一个不同的CheckboxComboBox单元格,它是emtpy(没有选中的项目)DataGridView单元格中的自定义控件
结果:新单元格的文本包含旧单元格的文本。如果我点击一个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();
}
任何想法如何解决这个问题?谢谢。
EDIT 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覆盖涂料()。当我在这个方法上放置一个断点时,当我单击单元格时它会被调用两次。第一次被调用时,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)
{}
它为什么会被调用两次,为什么在第二次调用它包含了正确的单元格错格式化值?
将DataGridViewCheckBoxComboBoxCell(s)添加到DataGridView时,代码的外观如何? –