0

我有一个应用程序,其中我的一个屏幕大量使用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我无论如何都要拦截的(出于原因)。但这不起作用——当用户选择一个单元格时,果然,错误图标消失了。我无法拦截和阻止DataGridViewCheckBoxCells 被选中SelectionChanged,因为选择是编辑单元格值过程中的第一步。

我能做些什么?

4

1 回答 1

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)
    {
    }
}
于 2015-04-01T14:26:47.810 回答