问dbGrid中实现指定行的显示颜色问题,我的dbgrid连着一个ibtable,我想在某些符合我要求的指定行设置颜色,以便和普通行有所区别,请问怎么实现啊?

解决方案 »

  1.   

    procedure TFrmuser.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
    if gdSelected in State then
    begin
      (Sender as TDBGrid).Canvas.Brush.Color :=$000080FF; //当前选中单元格显示红色
      DBGrid1.DefaultDrawColumnCell (Rect,DataCol,Column,State);
    end else
    begin
     //隔行改变网格背景色:
     if DBGrid1.DataSource.DataSet.RecNo mod 2 = 0 then
        (Sender as TDBGrid).Canvas.Brush.Color := $00EFF1FC //定义背景颜色
     else
        (Sender as TDBGrid).Canvas.Brush.Color := $00F3EADE;  //定义背景颜色
     DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;
    end;
      

  2.   

    先将DBGRID的options的dgselectRow属性设置为true!
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
            if state=[gdSelected] then
            //这是指被选择的行
            begin
              DBGrid1.Canvas.Font.Color:=clyellow;
              DBGrid1.Canvas.Brush.Color:=clred;
              //重新绘制这一行
              DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
            end;
    end;
      

  3.   

    procedure TFrmuser.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin  裡面是你的條件end;
      

  4.   

    procedure TFrmuser.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
     if DBGrid1.DataSource.DataSet.FieldByName('字段名').AsString = 'AA' then //条件
        (Sender as TDBGrid).Canvas.Brush.Color := $00EFF1FC //定义背景颜色
     DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;