请问如何给DBGRID特定行加上颜色?

解决方案 »

  1.   

    贴一段,这样的代码很多。搜一下就有。
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    if Table1.FieldByName('size').AsInteger >=40 then
      begin
      DBGrid1.Canvas.Font.Color := clRed;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      end;
    if (Table1.FieldByName('size').AsInteger >=30) and (Table1.FieldByName('size').AsInteger<40) then
      begin
      DBGrid1.Canvas.Font.Color := clBlue;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      end;
    if (Table1.FieldByName('size').AsInteger >=20) and (Table1.FieldByName('size').AsInteger<30) then
      begin
      DBGrid1.Canvas.Font.Color := clYellow;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      end;
    if (Table1.FieldByName('size').AsInteger >=10) and (Table1.FieldByName('size').AsInteger<20) then
      begin
      DBGrid1.Canvas.Font.Color := clLime;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      end;
    end;end.
      

  2.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if adoquery1.RecNo =10 then
        DbGrid1.Canvas.Brush.color:=rgb(0,0,125);
      DbGrid1.Canvas.pen.mode:=pmMask;
      DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    end;
      

  3.   

    好办
    在dbgrid的DBGrid1DrawColumnCell事件中改颜色就可以
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    datacol 是行标
    column是列标
      

  4.   

    上面几位能不能具体解释一下?比如说给DBGRID1DE的第二、三行改成黄色,共有四行。
      

  5.   

    据你的要求,自已想想吧。
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      dbgrid1.Canvas.Pen.Mode := pmmask;
      if adoquery1.RecNo = 2 then              //第2条记录,也是第2 行。
            Canvas.brush.Color := ClYellow;   //设定第2行背景为黄色
      dbgrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);  //绘制颜色。。
    end;end.