如何在当前DataGridViewCheckBoxCell上显示错误
本文关键字:显示 错误 DataGridViewCheckBoxCell | 更新日期: 2023-09-27 18:28:53
我有一个应用程序,其中一个屏幕大量使用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
中破解它,如果当前单元格是DirtyStateChanged
之后的DataGridViewCheckBoxCell
,我设法取消选择它(出于原因)。但这不起作用——当用户选择一个单元格时,错误图标就会消失。我无法拦截和阻止SelectionChanged
选择DataGridViewCheckBoxCell
s,因为选择是编辑单元格值的第一步。
我能做什么?
以下代码在每个包含错误文本的单元格中绘制一个"漂亮"的错误图标。您可能需要根据您的具体情况进行调整:
static Icon m_errorIcon;
public static Icon ErrorIcon
{
get
{
if (m_errorIcon == null)
{
ErrorProvider errorProvider = new ErrorProvider();
m_errorIcon = errorProvider.Icon;
errorProvider.Dispose();
}
return m_errorIcon;
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
try
{
if ((e.PaintParts & DataGridViewPaintParts.ErrorIcon) != 0 && !string.IsNullOrEmpty(e.ErrorText))
{
Icon icon = ErrorIcon;
int pixelMargin = 2;
Rectangle cellBounds = new Rectangle(e.CellBounds.X + pixelMargin, e.CellBounds.Y, e.CellBounds.Width - 2 * pixelMargin, e.CellBounds.Height);
Rectangle firstIconRectangle = new Rectangle(cellBounds.Left, cellBounds.Top + Math.Max((cellBounds.Height - icon.Width) / 2, 0), Math.Min(icon.Width, cellBounds.Width), Math.Min(icon.Width, cellBounds.Height));
e.Paint(e.ClipBounds, e.PaintParts & ~DataGridViewPaintParts.ErrorIcon);
e.Graphics.DrawIcon(icon, firstIconRectangle);
e.Handled = true;
}
}
catch (Exception exception)
{
}
}