【问题标题】:DataGridViewCell PaintErrorIcon methodDataGridViewCell PaintErrorIcon 方法
【发布时间】:2014-09-23 13:49:25
【问题描述】:

我正在尝试覆盖DataGridView 中某个列的errorIcon。我在网上找到了一些关于此的信息,但我的自定义类中的 PaintErrorIcon 方法永远不会被调用。为了测试,我为普通Paint 添加了一个覆盖,并使用下面的测试代码在我的输出中得到了“PAINT”,但是当我为单元格设置 errorText 时,我看不到“ERROR PAINT”(单元格当设置错误文本时,一定会得到一个错误图标并调用 Paint)。

public class DataGridViewWarningCell: DataGridViewTextBoxCell
{
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        Console.WriteLine("PAINT");
    }

    protected override void PaintErrorIcon(Graphics graphics, Rectangle clipBounds, Rectangle cellValueBounds, string errorText)
    {
        base.PaintErrorIcon(graphics, clipBounds, cellValueBounds, errorText);
        Console.WriteLine("ERROR PAINT");
    }
}

我已将列添加到我的 DataGridView 中,如下所示:

public class DataGridViewWarningColumn : DataGridViewColumn
{
    public DataGridViewWarningColumn()
    {
        this.CellTemplate = new DataGridViewWarningCell();
    }
}

然后在我的表单代码中:

var warningColumn = new DataGridViewWarningColumn();
fileGrid.Columns.Add(warningColumn);

【问题讨论】:

    标签: c# winforms datagridview datagridviewcolumn


    【解决方案1】:

    嗯,如果不轻轻一点,这似乎是行不通的..

    这是我尝试过的,但显然你会想要更改真实的图形内容..

    protected override void Paint(Graphics graphics, Rectangle clipBounds, 
              Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, 
              object value, object formattedValue, string errorText,
              DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle 
              advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        base.Paint( graphics, clipBounds, cellBounds, rowIndex, cellState, value, 
                    formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
         Console.WriteLine("PAINT");
         // call it by hand:
         if (errorText != "")  PaintErrorIcon(graphics, clipBounds, cellBounds, errorText);
    }
    
    protected override void PaintErrorIcon(Graphics graphics, 
                            Rectangle clipBounds, Rectangle cellValueBounds, string errorText)
    {
        // not the std icon, please
        //base.PaintErrorIcon(graphics, clipBounds, cellValueBounds, errorText);
        Console.WriteLine("ERROR PAINT");
        // aah, that's better ;-)
        graphics.FillRectangle(Brushes.Fuchsia, new Rectangle( clipBounds.Right - 10,   
                 cellValueBounds.Y + 3, clipBounds.Right, cellValueBounds.Height - 6));
     }
    

    我已关闭ShowCellErrors 并注释掉了对基本方法的调用。

    如果您无法为 DGV 关闭 ShowCellErrors,则必须小心完全覆盖标准图标,因为它仍然会被绘制,即使我们不调用 base.PaintErrorIcon。肯定是事情不如预期的另一个症状..

    我不确定要提交的最佳 Bounds Rectangle,但它似乎有所作为,所以这是一个开始..

    【讨论】:

    • 这就是我最终做的事情(目前正在尝试类似的东西,但您的输入使其完成,谢谢!)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多