首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在当前DataGridViewCheckBoxCell上显示错误?

如何在当前DataGridViewCheckBoxCell上显示错误?
EN

Stack Overflow用户
提问于 2014-11-04 02:12:31
回答 1查看 322关注 0票数 0

我有一个应用程序,其中一个屏幕大量使用DataGridViewCheckBoxCells。简而言之,我在VirtualMode中设置了包含撤消系统、验证等的表。在某些情况下,选中一个没有首先选中另一个框的框是没有意义的,以此类推。我允许用户这样做,但是我有一个检查函数来设置单元格的ErrorText属性,并且不允许它们继续。

诸如此类。我已经确认不是我的代码清除了ErrorText或者类似的东西。问题是,当错误图标/字形是当前单元格时,DataGridViewCheckBoxCells不要绘制错误图标/字形。我不明白为什么他们是这样设计的。实际上,参考资料(DataGridViewCheckBoxCell.cs)支持我的观点:

在DataGridViewCheckBoxCell.cs中:

代码语言:javascript
复制
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

代码语言:javascript
复制
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,因为选择是编辑单元格的值的第一步。

我能做什么?

EN

回答 1

Stack Overflow用户

发布于 2015-04-01 22:26:48

下面的代码在每个包含错误文本的单元格中绘制一个“漂亮的”错误图标。您可能需要根据您的特定情况对其进行调整:

代码语言:javascript
复制
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)
    {
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26720341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档