www.C-sharpcorner.com
 search datagrid
 or
 http://www.syncfusion.com/FAQ/WinForms/

解决方案 »

  1.   

    根据值得的不同显示不同的颜色
     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); 
     
                   } 
     
              } 
     
         } 
     
      

  2.   

    楼上的这样做法我看到过,好像要把DataGridColoredTextBoxColumn添加到DataGridStyle里面的。
    我说过,我是DataGrid直接绑定DataTable,根本没用到什么DataGridSytle。
    而且楼上的这种做法只能处理一列,好像两列就没办法了。
    因为对于不同的列,paint是一样的,但我用来判断的数据格式就有可能不一样。
    另外我的一张DataGrid绑定了不同的DataTable,用上面的方法恐怕会非常非常麻烦。
      

  3.   

    WebForm中
      DataGrid1.Items[i].Cells[j].BackColor = Color.Red;
    i,j 是第几行、第几列
      

  4.   

    WebForm中
      DataGrid1.Items[i].Cells[j].BackColor = Color.Red;
    i,j 是第几行、第几列
      

  5.   

    得到当前cell的句柄,就可以通过它的属性来对它的着色进行处理了
      

  6.   

    我也遇到类似的问题,关注中~!!!
    http://expert.csdn.net/Expert/topic/2110/2110057.xml?temp=.1420404
      

  7.   

    用得着大家写的那么麻烦吗?
    如下:
    DataGrid1.Items[1].Cells[1].Attributes["style"]="COLOR: #ff00ff";
      

  8.   

    to heroux(FlyerAero): 
    System.Windows.Forms.DataGrid 没有items属性,System.web中倒是有,只是我们现在做的时窗体应用程序!
      

  9.   

    继续up,没有人知道吗
    我C#用的时间不长,C#里还有句柄这个概念吗
    liduke(天下有雪) 能不能说具体点