很简单,自己控制行和列。因为在 TStringGrid 中每个 Cell 中的值都是 String, 处理对齐方式和字体显示方式都得自己控制。虽然是麻烦点,对于数据量少的用它还是蛮方便的。

解决方案 »

  1.   

    for I := 1 to StringGrid.ColCount//是从1开始
    for J := 1 to StringGrid.rowconnt//嘻嘻一样
    StringGrid.Cells[I,J] := FieldByName('..').AsString;
    编程
      

  2.   

    1、你先用 TQuery 或 TADOQuery 打开记录集;
    2、读取数据
       with Query1 do
          While not Eof do
          begin
             // 读取记录
             // ... ...
             Next;
          end;
    3、对于数据的显示在 StringGrid 的 OnDrawCell 事件中写程序,给你个例子。// 记录表格的 OnDrawCell 事件过程
    procedure TfrmShowRecord.strgridRecordDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
       strValue: String;
       intMargin, intHeight, intWidth: Integer;
    begin
       with Sender as TStringGrid do
       begin
          // 画背景
          if (ARow > 0) and (ACol > 0) then
          begin
             if ARow mod 2 = 0 then
                Canvas.Brush.Color := $00F4FFFE
             else
                Canvas.Brush.Color := clWhite;
             Canvas.FillRect(Rect);
          end;
          // 计算显示在矩形框中的左上角位置
          strValue := Cells[ACol, ARow];
          Canvas.Font := Font;
          intWidth := Canvas.TextWidth(strValue);
          if (ARow = 0) or (ACol = 0) then
          begin
             intMargin := (Rect.Right - Rect.Left - intWidth) div 2;
             if intMargin < 0 then
                intMargin := 0;
          end
          else
          begin
             intMargin := 2;
             // 根据列的对齐方式设置显示模式
             // 把对齐方式存放在数组中与列项相对应。
             if FarrColAlignments <> Nil then
                case FarrColAlignments[ACol - 1] of
                   taLeftJustify:
                      intMargin := 2;
                   taRightJustify:
                      intMargin := Rect.Right - Rect.Left - intWidth - 2;
                   taCenter:
                      intMargin := (Rect.Right - Rect.Left - intWidth) div 2;
                end;
          end;
          intHeight := (Rect.Bottom - Rect.Top - Canvas.TextHeight(strValue)) div 2;
          // 在矩形框中写值
          Canvas.TextRect(Rect, Rect.Left + intMargin, Rect.Top + intHeight, strValue);
       end;
    end;