现在有两个TStringGrid,A和B,行列数是一模一样,现在我需要的是当我点B的cell(如:cells[5,5])时改变自己的cell的颜色,同时改变A中cell[5,5]的颜色,不是指的某行,而是小个的单元格背景色

解决方案 »

  1.   

    给表格写如下事件procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if (ACol =5) and (ARow = 5) then
      begin
        StringGrid1.Canvas.Brush.Color := clRed;
        StringGrid1.Canvas.FillRect(Rect);
      end;
    end;
      

  2.   

    还改在后边加上
    Canvas.TextOut
    要不没文字 
      

  3.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      StringGrid2.Canvas.Brush.Color:=clWindow;
      StringGrid2.Canvas.FillRect(stringgrid1.CellRect(acol,arow));//恢复默认
    if (StringGrid1.Row =2) and (StringGrid1.Col = 2) then//去掉这行,点那个CEll,那个cell变
      begin
        StringGrid1.Canvas.Brush.Color := clRed;
        StringGrid1.Canvas.FillRect(stringgrid1.CellRect(StringGrid1.Col,StringGrid1.Row));
        stringgrid1.canvas.textout(rect.left,rect.top,StringGrid1.cells[acol,arow]);
        StringGrid2.Canvas.Brush.Color := clRed;
        StringGrid2.Canvas.FillRect(stringgrid2.CellRect(StringGrid1.Col,StringGrid1.Row));
        stringgrid2.canvas.textout(rect.left,rect.top,StringGrid1.cells[acol,arow]);
      end;
    end;
      

  4.   

    var
      str: string;
    begin
      if (ACol =5) and (ARow = 5) then
      begin
          Canvas.Brush.Color := clRed;
          Canvas.fillrect(ARect);
          str := trim(StringGrid1.Cells[ACol, ARow]);
          DrawText(Canvas.Handle, PChar(str), Length(str), ARect, DT_LEFT or DT_SINGLELINE or DT_VCENTER);
        end;
      end;
    end;
      

  5.   

    procedure TProductAndProviderFrm.DBGridEh2DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumnEh;
      State: TGridDrawState);
    begin
      inherited;
      with DBGridEh2 do
      begin
        if (Column.Index=DBGridEh2.FieldColumns['byBPRICE'].Index)
        and (DBGridEh2.DataSource.DataSet.FieldByName('byBPRICE').AsFloat>DBGridEh2.DataSource.DataSet.FieldByName('LOWPRICE').AsFloat*(1+0.02)) then
        begin
           Canvas.Brush.Color := clRed;
           DefaultDrawDataCell(Rect, Column.Field, State);
        end;
    end; 你改下吧 我用的是DBGridEh
      

  6.   

    不能改了.. 有好多东西要该.. 来不急了.. procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      R,Rex:TRect;
      fontCl:TColor;
      i,j:Integer;
      str:string;
    begin
      Caption:=Format('left:%d   top:%d  bott:%d  right:%d   ==== 行:%d 列:%d',[ R.Left,R.Top,r.Bottom,r.Right,ARow,acol]);  StringGrid2.Canvas.Brush.Color:=clWindow;
      StringGrid2.Canvas.FillRect(stringgrid1.CellRect(acol,arow));//恢复默认
      StringGrid1.Canvas.Brush.Color := clYellow;
      StringGrid1.Canvas.FillRect(stringgrid1.CellRect(StringGrid1.Col,StringGrid1.Row));  StringGrid2.Canvas.Brush.Color := clRed;
      StringGrid2.Canvas.FillRect(stringgrid2.CellRect(StringGrid1.Col,StringGrid1.Row));
      stringgrid2.canvas.textout(rect.left,rect.top,StringGrid2.cells[acol,arow]);end;
    这是事件code,点grd1点一个格时,grd2里有时文字会给覆盖.有什么好办法没有???