1.如何根据TStringGrid的Cell[]的值,给每个Cell设不同的背京色,如cell[2,2]=2时,绿色,=1时兰色。2.Tquery的afterscroll事件,如何在query打开前afterscroll事件赋为空,打开后又启用afterscroll事件

解决方案 »

  1.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      myColor:TColor;
    begin
      with StringGrid1 do
      begin
        if Cells[ACol,ARow]='' then  //颜色判断部分自己写吧.
          myColor:=clBtnFace         //
        else                         //
          myColor:=clBlack;          //  
        Canvas.brush.Color:=myColor;
        Canvas.FillRect(Rect);
      end;
    end;
      

  2.   

    好象没有字了..这样做不行...
    推荐使用topGrid....
      

  3.   

    1,
    应该是在onDrawCell里面var tmp:string;
        area:trect;
        i,j:integer;
    begin
     with StringGrid1.Canvas do
        begin
          i:=StringGrid1.Col;
          j:=StringGrid1.Row;
          rect:=stringgrid1.CellRect(i,j);
          FillRect(Rect);
          Area := rect;
          InflateRect(Area, -2, -2);
          tmp:=stringgrid1.Cells[i,j];
          StringGrid1.Canvas.Brush.Color:=clgradientactivecaption;
          TextRect(Area,i, j, StringGrid1.Cells[i,j]);
          DrawText(Handle, PChar(tmp),length(tmp),Area, DT_CENTER); 
        end;end;
      

  4.   

    如果不清楚,看看这个例子
    http://search.csdn.net/Expert/topic/1781/1781574.xml?temp=.5662805
      

  5.   

    这个就简单了
    改变某一个单元格的颜色问题
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var tmp: string;
        r_left : integer;
    begin
      tmp := sgridService.Cells[ACol,ARow];
      if ACol=0 then begin
         StringGrid1.Canvas.Brush.Color := clRed;
         StringGrid1.Canvas.FillRect(Rect);
         StringGrid1.Canvas.Font.Color := clBlack; //字体颜色
         //居中显示文字
         r_left := Rect.Left + (Rect.Right-Rect.Left-sgridService.Canvas.TextWidth(tmp)) div 2; 
         StringGrid1.Canvas.TextRect(Rect, r_left, Rect.Top + 6, tmp); //重画文字    
      end;
    end;