我有一个应用程序,其中一个屏幕大量使用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,我设法取消选择它(对于reasons)。但这不起作用--当用户选择一个单元格时,错误图标肯定会消失。我不能拦截和阻止用SelectionChanged选择DataGridViewCheckBoxCell,因为选择是编辑单元格的值的第一步。
我能做什么?
发布于 2015-04-01 22:26:48
下面的代码在每个包含错误文本的单元格中绘制一个“漂亮的”错误图标。您可能需要根据您的特定情况对其进行调整:
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)
{
}
}https://stackoverflow.com/questions/26720341
复制相似问题