DataGridViewCell PaintErrorIcon方法

时间:2014-09-23 13:49:26

标签: c# winforms datagridview datagridviewcolumn

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

1 个答案:

答案 0 :(得分: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,但似乎有所作为,所以这是一个开始......