在DataGrid的ItemDataBound事件里面写:
if((e.Item.Cells[5].Text)=="女")
{
e.Item.BackColor=Color.AliceBlue;
}
上面的意思是说,当DataGrid中第6列的值是“女”时,将这一行的背景色设置为AliceBlue。

解决方案 »

  1.   

    我在WINFORM中没有找到ItemDataBound事件啊,你是不是说的WEB?
      

  2.   

    我在.NET示例找到一个类似的例子,但是调试错误,怎么解决?
    public class Form1: Form
    {
     protected DataGrid dataGrid1;
     protected DataSet myDataSet;private void PaintCell(object sender, MouseEventArgs e)
    {
          
    // Use the HitTest method to get a HitTestInfo object.
        DataGrid.HitTestInfo hi;
        DataGrid grid = (DataGrid)sender;
        hi=grid.HitTest(e.X, e.Y);
        // Test if the clicked area was a cell.
        if(hi.Type == DataGrid.HitTestType.Cell)
        {
           // If it's a cell, get the GridTable and ListManager of the
           // clicked table.         
           DataGridTableStyle dgt = dataGrid1.TableStyles[0];  //我自己加的begin  myDataSet=bd.LoadBDTableByID("BD200204");
      dgt.MappingName="BD200204";   //我自己加的end       CurrencyManager cm = (CurrencyManager)this.BindingContext[myDataSet.Tables[dgt.MappingName]];
           // Get the Rectangle of the clicked cell.
           Rectangle cellRect;
           cellRect=grid.GetCellBounds(hi.Row, hi.Column);
           // Get the clicked DataGridTextBoxColumn.
           MyGridColumn  gridCol =(MyGridColumn)dgt.GridColumnStyles[hi.Column];
    //上面一句错误,信息“指定的转换无效”       // Get the Graphics object for the form.
           Graphics g = dataGrid1.CreateGraphics();
           // Create two new Brush objects, a fore brush, and back brush.
           Brush fBrush = new System.Drawing.SolidBrush(Color.Blue);
           Brush bBrush= new System.Drawing.SolidBrush(Color.Yellow);
           // Invoke the Paint method to paint the cell with the brushes.
           gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush, false);
         }
     }
    }public class MyGridColumn:DataGridTextBoxColumn{
       public void PaintCol(Graphics g, Rectangle cellRect, 
          CurrencyManager cm, int rowNum, Brush bBrush, 
          Brush fBrush, bool isVisible){
          this.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible);
       }
    }
      

  3.   

    C#我刚开始,但我知道Delphi 或VB里面,对这个问题的解决是很简单的,你可以在程序里面利用判断的方法将他的text颜色和backgroud颜色设置成不同的颜色.