stringgrid 共10多列,NNN行,单元格里面的数据是的字符串是0,1,2,根据数字不同单元格显示不同的颜色。比如0的单元格是红色,1的是绿色,当然0,1的颜色都是黑色,如何处理?procedure TForm_gltjxx.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var i,j :integer;
begin  for i:=0 to stringgrid1.RowCount do
    begin
      for j :=0 to stringgrid1.ColCount do
        begin
        if  stringgrid1.Cells[j,i] = '0' then
          begin
            //Tstringgrid(sender).Canvas.Brush.Color := cllime;
            stringgrid1.Canvas.Brush.Color := cllime;
            stringgrid1.Canvas.Font.Color := clred;
            //TStringGrid(Sender).Canvas.FillRect(Rect);//补充这一句,否则cell中将会显示一部分旧的数据
            //DrawText(TStringGrid(Sender).Canvas.Handle, PChar(stringgrid1.Cells[j,i]), Length(stringgrid1.Cells[j,i]), Rect, DT_CENTER);
          end;
        if   stringgrid1.Cells[j,i] = '1' then
          begin
            //Tstringgrid(sender).Canvas.Brush.Color := clred;
            //TStringGrid(Sender).Canvas.FillRect(Rect);//补充这一句,否则cell中将会显示一部分旧的数据
            //DrawText(TStringGrid(Sender).Canvas.Handle, PChar(s), Length(s), Rect, DT_CENTER);
          end;        end;
    end;
end;上面是论坛里面收出来的结果,但是无论怎么弄,都达不到我要的效果。

解决方案 »

  1.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      If (ACol > 0) and (ARow>0) then
      begin
        if StringGrid1.Cells[ACol, ARow] = '0' then
          StringGrid1.Canvas.Brush.color := clRed;
        if StringGrid1.Cells[ACol, ARow] = '1' then
          StringGrid1.Canvas.Brush.color := clBlue;
        StringGrid1.canvas.fillRect(Rect);
        StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
      end;
    end;procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      StringGrid1.Cells[1, 1] := '0';
      StringGrid1.Cells[1, 2] := '1';
    end;