我用TStringGrid的OnDrawCell事件来给网格中的字体改变颜色,每个单元格的数据和stringlist中的数据比较,不同就将单元格字体显示蓝色,相同显示红色,当写入单元格中的数据较少时,一般少于100多时运行显示都很正常,当大于200时显示就不正常,有时应该部分显示红色但全部显示红色,有时还一直在动态显示中稳定不小来也停止不了运行。求解。
procedure  TFrmDataAnalyse.StringGrid1DrawCell(Sender:   TObject;   ACol,   ARow:   Integer;
    Rect:   TRect;   State:   TGridDrawState);
var
 x,y,m,n:integer;
 s:string;
begin
  //下面是绘制单元格颜色,相同时为红色
     with   StringGrid1   do
     begin
          n:=StringGrid1.RowCount;
          m:=StringGrid1.ColCount;
          for y:=1 to m-1 do
          begin
           for x:=1 to n-1 do
             begin
             rect:= StringGrid1.CellRect(y,x);
             s:= StringGrid1.Cells[y,x];
              if pos(s,emptyaddr.CommaText)=0 then  //emptyaddr.CommaText 内容为其他过程获得一般数据比较多
               begin
                StringGrid1.Canvas.Font.Color:=clBlue;
                StringGrid1.Canvas.FillRect(rect);
               end
              else
               begin
                StringGrid1.Canvas.Font.Color:=clRed;
                StringGrid1.Canvas.FillRect(rect);
               end;
               drawtext(StringGrid1.Canvas.Handle,pchar(s),length(s),rect, DT_CENTER);
               //StringGrid1.Canvas.TextOut(rect.Left+2,rect.Right+2,Top+2,s);
               if s = inttostr(last) then break;
             end;
           end;
      end;
end;

解决方案 »

  1.   

    数据量大了,刷新数据量比较大,你可以试一下CXGRID...DEV控件
      

  2.   

    你的实现有问题,不该在这个事件里一次性画完。onDrawCell是一个单元格一个单元触发的,数据量一大,就完蛋了
      

  3.   

    DrawCell  尽量少做一点处理.
    比较可以放到外面先比较好.然后用个数组先记录着
    本来 string的效率就不是很高.你还循环
      

  4.   

    效率的瓶颈在下面:
     if pos(s,emptyaddr.CommaText)=0 then //emptyaddr.CommaText 内容为其他过程获得一般数据比较多
    如果单纯的画,一万个也不是问题。
      

  5.   

    OnDrawCell用错了,它是一个CELL一个CELL的画的,你现在全部循环重画了,而且每个Cell可以绑定一个Object,你可以写个类保存自己的数据,画的时候取出来判断就行了,不要每次画都去查数据
      

  6.   

     我现在把比较放到外面的函数了,做法是在在循环写cell()单元格的时候就比较,要是有相同的就运行StringGrid1.Font.Color:=clRed否则就为蓝色,画的函数如下:
    procedure  TFrmDataAnalyse.StringGrid1DrawCell(Sender:   TObject;   ACol,   ARow:   Integer; Rect: TRect; State: TGridDrawState);
    var
      s:string;
    begin
          with   self.StringGrid1   do
         begin
              StringGrid1.Canvas.FillRect(rect);
               s:= StringGrid1.Cells[ACol,ARow];
               drawtext(StringGrid1.Canvas.Handle,pchar(s),length(s),rect, DT_CENTER);
          end;
    end;
     但是运行结果是所以的数据都显示蓝色,包括没有纳入比较的0行和0列数据也跟着显示蓝色,这是怎么回事呢,OnDrawCell这个事件到底是怎样一个个画的?应该怎样操作才对呀.
      

  7.   

        if (ACol=0)or(ARow=0) then exit;
        with    Sender as TStringGrid do
        begin
            if ARow mod 2 =0 then
            begin
               Canvas.Brush.Color :=clMoneyGreen;
               Canvas.Font.Color := clGreen;
            end
            else begin
               Canvas.Brush.Color :=clwhite;
               Canvas.Font.Color := clBlack;
            end;
            if gdSelected in State then
            begin
                Canvas.Brush.Color :=clHighlight;
                Canvas.Font.Color := clHighlightText;
            end;
            Canvas.FillRect(Rect);
            Canvas.TextOut (Rect.Left,Rect.Top ,Cells[ACol,ARow]);
        end;画整行的