2011-03-29 75 views
0

如何更改DataGrid中行的字體顏色?DataGrid中行的字體顏色

顏色取決於表中的條件。

我看到this發佈在這裏stackflow但它只適用於選定的行,並且該行將是另一種顏色,無論選擇或不。

回答

0

您可以在網格視圖的Paint中添加事件處理程序。

如果你想做的不僅僅是顏色,我們已經走了從DataGridViewCell繼承的路線,並覆蓋其Paint方法,從DataGridViewColumn繼承使用該單元格,然後在我們的網格視圖中使用該列。

下面是重寫的方法,但事件hander看起來很相似。

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) 
     { 
      if ((value as WhatEverType).WhatEverField == 9) 
      { 
       cellStyle.ForeColor = Color.CornflowerBlue; 
      } 
      base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 
     } 
+0

我不明白。 我將創建一個類,從DataGridViewCell繼承它並重寫Paint ..但那麼是什麼? – ridermansb 2011-03-29 17:54:15

+0

請記住,我正在開發環境Compact Framework(Windows Mobile) – ridermansb 2011-03-29 17:56:17

+0

也許你只需要添加一個事件處理程序,但是如果你想繼承,你繼承了一個按你想要的方式繪製的單元格,你繼承了一個使用您的單元格,並在網格視圖中使用該列。可能矯枉過正的字體顏色變化,但我們也在做其他所有的事情。我不確定這對Windows Mobile是否有用。 – 2011-03-29 19:11:01