我有一个应用程序,其中一个屏幕大量使用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
我正在截取它(对于reasons)。但是这不起作用 - 当用户选择一个单元格时,肯定会有错误图标消失。我无法拦截和阻止DataGridViewCheckBoxCell
被SelectionChanged
选中,因为选择是编辑单元格值的第一步。
我该怎么办?
答案 0 :(得分:0)
以下代码在每个包含错误文本的单元格中绘制“漂亮”错误图标。您可能需要根据具体情况进行调整:
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)
{
}
}