2011-03-29 76 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