如何根据特定值改变cell的颜色
 public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn 
 
     { 
 
          protected override void Paint(System.Drawing.Graphics g, 
 
               System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager 
 
               source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush 
 
               foreBrush, bool alignToRight) 
 
          { 
 
          // the idea is to conditionally set the foreBrush and/or backbrush 
 
          // depending upon some crireria on the cell value 
 
          // Here, we color anything that begins with a letter higher than 'F' 
 
               try{ 
 
                    object o = this.GetColumnValueAtRow(source, rowNum); 
 
                    if( o!= null) 
 
                    { 
 
                         char c = ((string)o)[0]; 
 
                         if( c > 'F') 
 
                         { 
 
                         // could be as simple as 
 
                         // backBrush = new SolidBrush(Color.Pink); 
 
                         // or something fancier... 
 
                              backBrush = new LinearGradientBrush(bounds, 
 
                                   Color.FromArgb(255, 200, 200), 
 
                                   Color.FromArgb(128, 20, 20), 
 
                                   LinearGradientMode.BackwardDiagonal); 
 
                              foreBrush = new SolidBrush(Color.White); 
 
                         } 
 
                    } 
 
               } 
 
                catch(Exception ex){ /* empty catch */ } 
 
               finally{ 
 
                    // make sure the base class gets called to do the drawing with 
 
                    // the possibly changed brushes 
 
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); 
 
               } 
 
          } 
 
     } 
 

解决方案 »

  1.   

    你看看这里可能有用
    How do I color a individual cell depending upon its value or some external method?
    5.10
      

  2.   

    5.10 How do I color a individual cell depending upon its value or some external method?
    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q745q
      

  3.   

    这些地方俺都去过了,根据这种方法只能一次生成列样式DataGridTextBoxColumn aColumnColumn = new DataGridTextBoxColumn();我现在要根据数据库中
    的预警值生成某个cell的样式!!!