in CB, do this way:void __fastcall TForm1::StringGrid1DrawCell(.....){
      if(Row==MyIntendedRow){
              StringGrid1->Canvas->Brush->Color=clRed; //Replace with your choice
               StringGrid1->Canvas->FillRect(Rect);
      }
      DrawText(StringGrid1->Canvas->Handle,
               StringGrid1->Cells[Col][Row].c_str(),
               -1, (RECT*)&Rect,
               DT_SINGLELINE | DT_VCENTER |DT_CENTER
      );
}
Delphi is exactly the same as CB on this point, DIY.

解决方案 »

  1.   

    以下代码我试过了:procedure TForm3.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if (ARow=2) and (ACol=2) then
      with TStringGrid(Sender).Canvas do
      begin
         Brush.Color := clRed;
         FillRect(Rect);
         TextOut(Rect.Left, Rect.Top, TStringGrid(Sender).Cells[ACol, ARow]);
      end;
    end;procedure TForm3.Button1Click(Sender: TObject);
    begin
      //StringGrid1.RowCount := 5;
      Stringgrid1.Cells[2,2]:= 'aaaa';
      Stringgrid1.Cells[3,2]:= 'bbbb';
      Stringgrid1.Cells[2,3]:= 'cccc';
      Stringgrid1.Cells[3,3]:= 'dddd';end;
      

  2.   

    就是呀,小兵,你的例子能出现效果证明我的代码没问题呀,但在我的程序中,我设了断点来跟踪,DrawCell事件根本没有执行过,天呀!怎么回事,对了,附带说一句,TStringGrid所在的窗体也是动态创建的,但这个应该没多大关系呀,是不是TStringGrid里有些属性设错了呢?但我试来试去都没办法。
      

  3.   

    我试了一下,没发现什么问题,下面是我的代码,StringGrid1的属性都是默认的。procedure SetRowColor(Sender:TObject;ACol, ARow: Integer;Rect: TRect);
    ...
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);begin
        SetRowColor(Sender,ACol,ARow,Rect);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      I, J, K : Integer;
    begin
      K := 0;
      with StringGrid1 do
        for I := 0 to ColCount - 1 do
          for J:= 0 to RowCount - 1 do
            begin
              K := K + 1;
              Cells[I,J] := IntToStr(K+100);
            end;
    end;procedure TForm1.SetRowColor(Sender: TObject;ACol, ARow: Integer;Rect: TRect);
    begin
        with TStringGrid(Sender).Canvas do
        begin
            if ARow=TStringGrid(Sender).RowCount-1 then
            begin
                Brush.Color:= clRed;
                FillRect(Rect);
                TextOut (Rect.Left+3,Rect.Top+4,TStringGrid(Sender).Cells[ACol,ARow]);
            end;
        end;
    end;
      

  4.   

    还是不行,不知道为什么,从Form创建开始,StringGrid的OnDrawCell事件就根本没有执行过,我新建一个应用程序,加入TStringGrid,上面大家所说的都能用,但我原来的应用程序就是不行,不知道谁有类似的经验,可以指点一下吗?
      

  5.   

    你有没有将所创建的StringGrid的onDrawCell事件与你自己定义的关联起来?
    如:
       MyStringGrid.onDrawCell:=MyDrawCell;
      

  6.   

    没有这回事,我没有自定义事件,都是用Delphi默认的。
      

  7.   

    在DrawCell事件中
    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clRED;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  8.   

    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clred;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  9.   

    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clAqua;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  10.   

    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clred;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  11.   

    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clred;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  12.   

    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clred;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  13.   

    with SG_ProjectInvest do
       begin
    if Cells[0,ARow]='' then
       begin
         Canvas.Font.Color:=clBlack;
        Canvas.Brush.Color:=clred;
        Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
       end;
       end;
      

  14.   

    照coldljy(凤舞N天)所说的,加上“StringGrid1.onDrawCell:=StringGrid1DrawCell”还是不行,程序硬是从来不执行OnDrawCell语句。即使只有短短一句话:"Showmessage('abcd');"也执行不了,想在Form的Show的过程中显式地调用Form1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);却不知道括号内的参数怎么赋值,真的没有任何办法了吗?
      

  15.   

    你的代码在某处有问题,建议调试步骤:
    1、用一个非动态创建的StringGrid, 将其OnDrawCell事件与你的事件处理代码关联,看是否会执行,如是,goto 2,如否,看看时那个属性设置问题
    2. 使用动态创建的StringGrid,将其OnDrawCell事件与你的事件处理代码关联,看是否会执行。如否,检查哪个属性设置与前一个StringGrid不同
      

  16.   

    我的是bcb我是我程序里的,没有问题
    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
          int ARow, TRect &Rect, TGridDrawState State)
    {         StringGrid1->Canvas->Brush->Color=clRed;
          StringGrid1->Canvas->FillRect(Rect);
          StringGrid1->Canvas->Font->Color=clBlue;*/
     //-------------
           int x;
     if(StringGrid1->Cells[ACol][ARow]=="ftp")
    //可以变为 if(StringGrid1->Cells[ACol][ARow]->AsInteger>50)
         {     StringGrid1->Canvas->Font->Color = clBlue;
         StringGrid1->Canvas->Brush->Color = clRed;
         StringGrid1->Canvas->FillRect(Rect);
         StringGrid1->Canvas->TextOutA(Rect.left,Rect.top,StringGrid1->Cells[ACol][ARow]);
          }
       else{     StringGrid1->Canvas->Font->Color = clBlack;
         StringGrid1->Canvas->FillRect(Rect);
         StringGrid1->Canvas->TextOutA(Rect.left,Rect.top,StringGrid1->Cells[ACol][ARow]);
       }
    }