我有一个应用程序,其中我的一个屏幕大量使用DataGridViewCheckBoxCells
. 简而言之,我在表格中设置了VirtualMode
涉及撤消系统、验证等。在某些情况下,选中一个没有先选中另一个框的框是没有意义的,依此类推。我让用户这样做,但我有一个检查功能,可以设置单元格的ErrorText
属性并且不让他们继续。
呸呸呸。我已经确认不是我的代码清除了ErrorText
或类似的东西。问题是DataGridViewCheckBoxCells
当它是当前单元格时不要绘制错误图标/字形。我无法解释为什么它们是这样设计的。事实上,参考源(DataGridViewCheckBoxCell.cs)支持我:
在 DataGridViewCheckBoxCell.cs 中:
protected override Rectangle GetErrorIconBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
{
// some checks ...
Point ptCurrentCell = this.DataGridView.CurrentCellAddress;
if (ptCurrentCell.X == this.ColumnIndex &&
ptCurrentCell.Y == rowIndex && this.DataGridView.IsCurrentCellInEditMode)
{
// PaintPrivate does not paint the error icon if this is the current cell.
// So don't set the ErrorIconBounds either.
return Rectangle.Empty;
}
// behavior ...
}
并看看DataGridViewCheckBoxCell.PaintPrivate
:
private Rectangle PaintPrivate(Graphics g,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates elementState,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts,
bool computeContentBounds,
bool computeErrorIconBounds,
bool paint)
{
// blah blah
// here it determines "If I am the current cell DO NOT draw the error icon!"
Point ptCurrentCell = this.DataGridView.CurrentCellAddress;
if (ptCurrentCell.X == this.ColumnIndex &&
ptCurrentCell.Y == rowIndex && this.DataGridView.IsCurrentCellInEditMode)
{
drawErrorText = false;
}
// sure enough, drawErrorText must be true here for the error icon to be drawn
if (paint && DataGridViewCell.PaintErrorIcon(paintParts) && drawErrorText && this.DataGridView.ShowCellErrors)
{
PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, errorBounds, errorText);
}
}
我怎样才能解决这个问题?我试图子类DataGridViewCheckBoxCell
化无济于事。有很多内部函数和常量我打不过。我试图在Form
使用这个表的那个中破解它,并且我设法取消选择当前单元格,如果它是DataGridViewCheckBoxCell
在它之后DirtyStateChanged
我无论如何都要拦截的(出于原因)。但这不起作用——当用户选择一个单元格时,果然,错误图标消失了。我无法拦截和阻止DataGridViewCheckBoxCell
s 被选中SelectionChanged
,因为选择是编辑单元格值过程中的第一步。
我能做些什么?