2014-09-23 55 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

相關問題