如何改变dbgrid的整行颜色????????????????????????????例如 dbgrid 有如下字段    姓名   性别    学号
现在要求  如果 性别 为 男 则把 整行的颜色 改为 蓝色 ,如果 性别 为 男 则把 整行的颜色 改为 红 色 .  
注意是改变整行的颜色,不是一个单元格的颜色!!!请给出详细的代码,谢谢

解决方案 »

  1.   

    type 
    TMyCustomDBGrid = class(TCustomDBGrid); procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
    var
    sCjts,sQjTs:String;
    begin
    with TMyCustomerDBGrid(Sender) do
    begin
    Cjts:=DataLink.Fields[5].AsString;
    sQjts:=DataLink.Fields[9].AsString;
    if sCjts<>‘‘ then       //春季退书数量>0的用红色显示
    Canvas.Brush.Color := clRed
    else
    if sQjts<>‘‘ then      //秋季退书数量>0的用黄色显示
    Canvas.Brush.Color := clYellow
    else
    Canvas.Brush.Color:=clWhite;
    Canvas.Font.Color:=clBlack; 
    canvas.fillrect(rect);
    canvas.textout(rect.left+4,rect.top+4,Column.Field.AsString);
    end;
    end;
      

  2.   

    在DBGrid的DrawColumnCell 事件中写一下代码:
    If trim(ADOQ1.fieldbyname('xingbie').AsString)='男' then
     Begin
       DBGrid1.Canvas.Brush.Color := clblue  ;
       DBGrid1.DefaultDrawColumnCell( Rect, DataCol, Column, State);
     End
     else
     begin
       DBGrid1.Canvas.Brush.Color := clred;
       DBGrid1.DefaultDrawColumnCell( Rect, DataCol, Column,State);
     end;
      

  3.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
    var
    Grid :TDBGrid;
    Data :TDataset;
    begin  Grid := TDBGrid(Sender); 
      Data := TDBGrid(Sender).DataSource.DataSet;  If trim(Data.fieldbyname('性别').AsString)='男' then
      Begin
         Grid.Canvas.Brush.Color := clblue  ;
         Grid.DefaultDrawColumnCell( Rect, DataCol, Column, State);
      End
      else if trim(Data.fieldbyname('性别').AsString)='女' then 
      begin
         Grid.Canvas.Brush.Color := clred;
         Grid.DefaultDrawColumnCell( Rect, DataCol, Column,State);
      end
      else
         Grid.DefaultDrawColumnCell( Rect, DataCol, Column,State);end;
      

  4.   


    還要把DBGrid1->Options->dgRowSelect設為True.
      

  5.   

    DBGrid中行的定位及着色实现
    http://www.pconline.com.cn/pcedu/empolder/gj/delphi/0402/320350.html
      

  6.   

    先把DBGrid1->Options->dgRowSelect设为True,然后在DrawColumnCell事件里写:
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin  
      if adoquery1.FieldByName('sex').AsString='男' then  DBGrid1.Canvas.Font.Color:=clGreen
      else if adoquery1.FieldByName('sex').AsString;='女' then  DBGrid1.Canvas.Font.Color:=clBlue;
      DBGrid1.DefaultDrawColumnCell(rect,DataCol,column,state);
    end;