在Delphi中DBGrid中的列高 [英] Column height in DBGrid in Delphi

查看:114
本文介绍了在Delphi中DBGrid中的列高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在delphi 7中使用DBgrid组件。

我想将表列显示到网格中。

表列是 queryId,empid,empname和查询查询列的数据类型为文本。

I am using DBgrid Component in delphi 7.
I want to display my table columns to grid.
Table Columns are queryId,empid,empname and Query. Query column has datatype as Text.

查询列可能包含一个长字符串。但是它显示在一行中。

我需要固定列的宽度,并且根据数据的高度,特定行的高度会有所不同。

The Query column may contain a long string. But it's displayed in a single line.
I need to fix the width of columns and depending upon the data the height will vary for that particular row.

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

Is it possible in DBGrid to change the height of a row like it allows multiline in single record similar to Excel?

推荐答案

查看代码。它用于TStringGrid,但是您知道DBGrid和StringGrid是从TCustomGrid派生的。这样您就可以将代码用于DBGrid。

Look this code. It's for TStringGrid, but you know DBGrid and StringGrid are derived from TCustomGrid. Sou you can use the code for 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; 

这篇关于在Delphi中DBGrid中的列高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆