2014-09-23 56 views
1

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

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

嗯,好像这不会没有工作有点轻推..

这是我试过的,但你会想改变真正的图形的东西,显然..

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,您也必须完整地修复标准图标,因为它仍然被绘制。毫无疑问的一些事情并不像预期的另一种症状..

我不知道最好的边界矩形交,但似乎做一些事情,所以这是一个开始..

+0

这是我结束(现在正在试验类似的东西,但你的意见完成了,谢谢!) – 2014-09-25 11:03:46

相关问题