如何设置单元格Changing the background color of cells in a DataGrid
http://www.codeproject.com/csharp/custom_datagridcolumnstyl.asp?target=datagrid如何设置行颜色:这里面有我写的代码:
http://search.csdn.net/expert/topic/52/5201/2002/11/26/1207892.htm

解决方案 »

  1.   

    5.10 How do I color a individual cell depending upon its value or some external method?     We give three different methods for doing this.
    The first one overrides Paint in a derived columnstyle and sets the backcolor there. 
    The second uses a delegate to set the color in the Paint override. 
    The third method adds an event to the derived column style to allow you to set the color in an event handler.Method 1
    You can do this by deriving from DataGridTextBoxColumn and overriding the Paint method to conditionally set the backColor and foreColor. The sample code below colors any cell that starts with a letter higher than 'F'. You can download a project (C#, VB) using this class. 
     
    [C#] 
     
         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); 
     
                   } 
     
              } 
     
         }