【问题标题】:Column height in DBGrid in DelphiDelphi中DBGrid的列高
【发布时间】:2011-12-22 09:31:33
【问题描述】:

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

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

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

【问题讨论】:

  • SMDBGrid 组件是否能够包装 Recore 列?

标签: delphi delphi-7


【解决方案1】:

查看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; 

【讨论】:

  • 感谢回复.. SetGridHeight 是 TstringGrid 过程还是什么??
  • 我在互联网上看到过这个过程的变体,但没有人解释什么是 SetGridHeight
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
相关资源
最近更新 更多