Delphi中DBGrid中的列高

时间:2011-12-22 09:31:34

标签: delphi delphi-7

我在delphi 7中使用DBgrid组件。
我想将表格列显示为网格 表列为queryId,empid,empname and QueryQuery列的数据类型为Text。

“查询”列可能包含长字符串。但它显示在一行中 我需要修复列的宽度,并根据数据,该特定行的高度会有所不同。

在DBGrid中是否可以更改行的高度,就像它允许单行记录中的多行类似于Excel?

1 个答案:

答案 0 :(得分:0)

查看this代码。它适用于TStringGrid,但您知道DBGrid和StringGrid是从TCustomGrid派生的。你可以使用DBGrid的代码。

procedure DrawSGCell(Sender : TObject; C, R : integer; Rect : TRect; 
          Style : TFontStyles; Wrap : boolean; Just : TAlignment; 
          CanEdit : boolean); 
  { draws formatted contents in string grid cell at col C, row R; 
    Style is a set of fsBold, fsItalic, fsUnderline and fsStrikeOut; 
    Wrap invokes word wrap for the cell's text; Just is taLeftJustify, 
    taRightJustify or taCenter; if CanEdit false, cell will be given 
    the background color of fixed cells; call this routine from 
    grid's DrawCell event } 
var 
  S        : string; 
  DrawRect : TRect; 
begin 
  with (Sender as tStringGrid), Canvas do begin 
    { erase earlier contents from default drawing } 
    if (R >= FixedRows) and (C >= FixedCols) and CanEdit then 
      Brush.Color:= Color 
    else 
      Brush.Color:= FixedColor; 
    FillRect(Rect); 
    { get cell contents } 
    S:= Cells[C, R]; 
    if length(S) > 0 then begin 
      case Just of 
        taLeftJustify  : S:= ' ' + S; 
        taRightJustify : S:= S + ' '; 
        end; 
      { set font style } 
      Font.Style:= Style; 
      { copy of cell rectangle for text sizing } 
      DrawRect:= Rect; 
      if Wrap then begin 
        { get size of text rectangle in DrawRect, with word wrap } 
        DrawText(Handle, PChar(S), length(S), DrawRect, 
          dt_calcrect or dt_wordbreak or dt_center); 
        if (DrawRect.Bottom - DrawRect.Top) > RowHeights[R] then begin 
          { cell word-wraps; increase row height } 
          RowHeights[R]:= DrawRect.Bottom - DrawRect.Top; 
          SetGridHeight(Sender as tStringGrid); 
          end 
        else begin 
          { cell doesn't word-wrap } 
          DrawRect.Right:= Rect.Right; 
          FillRect(DrawRect); 
          case Just of 
            taLeftJustify  : DrawText(Handle, PChar(S), length(S), DrawRect, 
                               dt_wordbreak or dt_left); 
            taCenter       : DrawText(Handle, PChar(S), length(S), DrawRect, 
                               dt_wordbreak or dt_center); 
            taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect, 
                               dt_wordbreak or dt_right); 
            end; 
          end 
        end 
      else 
        { no word wrap } 
        case Just of 
          taLeftJustify  : DrawText(Handle, PChar(S), length(S), DrawRect, 
                             dt_singleline or dt_vcenter or dt_left); 
          taCenter       : DrawText(Handle, PChar(S), length(S), DrawRect, 
                             dt_singleline or dt_vcenter or dt_center); 
          taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect, 
                             dt_singleline or dt_vcenter or dt_right); 
          end; 
      { restore no font styles } 
      Font.Style:= []; 
      end; 
    end; 
end;