如第0列要居中 即 行号  列
第0行要居中   即 字段名 行第1、2列内容要左对齐第3列内容要右对齐初始化
procedure TForm1.FormCreate(Sender: TObject);
var i,j : integer;
begin    Stringgrid1.RowCount:=4;
    stringgrid1.colcount:=4;
  //行号列
  stringgrid1.Cells[0,0]:='行号';
  for i:=1 to StringGrid1.RowCount-1 do
  stringgrid1.Cells[0,i]:=inttostr(i);  //字段名行
  for i:=1 to StringGrid1.ColCount-1 do
  stringgrid1.Cells[i,0]:='字段' + inttostr(i);  //内容
  for i:=1 to StringGrid1.RowCount-1 do
   for j:=1 to StringGrid1.ColCount-1 do
   StringGrid1.Cells[j,i]:=inttostr(i*j+100);end;

解决方案 »

  1.   

    //首行居中,其他靠右,仅供参考
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var CellStr: String;
        Mode: Integer;
    begin
      if ARow=0 then
        Mode := DT_CENTER      //調整左右
      else
        Mode := DT_RIGHT;
      Rect.Top := Rect.Top+3;  //調整上下
      
      CellStr := StringGrid1.Cells[ACol,ARow];
      StringGrid1.Canvas.FillRect(Rect);
      DrawText(StringGrid1.Canvas.Handle,PChar(CellStr),Length(CellStr),Rect,Mode);
    end;
      

  2.   

    在StringGrid 的OnDrawCell事件下面写入代码(下面是居中的例子,默认居左,居右类似)
    var
      CX, CY:Integer;
      Str:string;
    begin
      Str := StringGridInfo.cells[acol, arow];
      CX := (Rect.Right - Rect.Left - Self.StringGridInfo.Canvas.TextWidth(Str)) div 2;
      CY := (Rect.Bottom - Rect.Top - Self.StringGridInfo.Canvas.TextHeight(Str)) div 2;
      StringGridInfo.Canvas.TextOut(rect.left + CX, rect.top + CY, Str);
    end;