先在StringGrid1录入一次,然后在StringGrid2录入第二次,如果StringGrid2.Cells[StringGrid2.Col,StringGrid2.Row] 不等于  StringGrid1.Cells[StringGrid2.Col,StringGrid2.Row]
让StringGrid2.Cells[StringGrid2.Col,StringGrid2.Row]的单元格文字显示红色 procedure TDataimportForm.StringGrid2KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
beginif  
StringGrid2.Cells[StringGrid2.Col,StringGrid2.Row] <>  StringGrid1.Cells[StringGrid2.Col,StringGrid2.Row]  then
   begin
   messagebeep(0);
   让StringGrid2.Cells[StringGrid2.Col,StringGrid2.Row]单元格文字显示红色
end;怎样实现 

解决方案 »

  1.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    自绘
      

  2.   

    或许可以在OnSetEditText里着手处理
    advstringgrid里好像有validate处理事件,能更方便些
      

  3.   

    在StringGrid1DrawCell只能绘制出最近一次不相同的单元格,以前的会丢失,能给个具体的办法不
      

  4.   

    最好在grid2中增加一列用来保存grid1中对应的文字,这样即可以用drawcell事件中处理起来就比较方便了
      

  5.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        StringGrid2: TStringGrid;
        procedure StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
      private
        { Private declarations }
        procedure SetGridColor(ARow,ACol: Integer;Rect: TRect;StrGrid,StrGrid2: TStringGrid);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.SetGridColor(ARow,ACol: Integer;Rect: TRect;StrGrid,StrGrid2: TStringGrid);
    var
      strValue:  String;
    begin
      with StrGrid do
      begin
       //画背景
       if  (ARow > 0) and (ACol > 0)  then
       begin
         if StrGrid.Cells[ACol,ARow]<>StrGrid2.Cells[ACol,ARow] then
           Canvas.Font.Color  := clRed
         else
           Canvas.Font.Color  := clBlack;
         Canvas.FillRect(Rect);
       end;
       //计算显示在矩形框中的左上角位置
       strValue  :=  Cells[ACol,ARow];
       //在矩形框中写值
       Canvas.TextRect(Rect,Rect.Left+5,Rect.Top+(Rect.Bottom-Rect.Top-Canvas.TextHeight(strValue)) div 2,strValue);
      end;
    end;procedure TForm1.StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      SetGridColor(ARow,ACol,Rect,StringGrid2,StringGrid1);
    end;end.