StringGrid使用全书( 一)

本文介绍了如何使用Delphi中的StringGrid组件,包括设置最佳列宽以避免文字截断,删除和插入列的操作,以及实现行的排序功能。通过示例代码展示了具体实现方法,如SetOptimalGridCellWidth过程用于调整列宽,GridRemoveColumn和GridAddColumn用于行列操作,而GridSort则实现了基于特定列的排序功能。
摘要由CSDN通过智能技术生成

(1)正确地设置StringGrid列宽而不截断任何一个文字方法是在对StringGrid填充完文本串后调用SetOptimalGridCellWidth过程
-----------程序片断-------------------------------------------------
 (*
 $Header$
 Module Name : General/BSGrids.pas
 Main Program : Several.
 Description : StringGrid support functions.
 03/21/2000 enhanced by William Sorensen
 *)

 unit BSGrids;

 interface

 uses
   Grids;

 type
   TExcludeColumns = set of 0..255;
   procedure SetOptimalGridCellWidth(sg: TStringGrid;
   ExcludeColumns: TExcludeColumns);
   // Sets column widths of a StringGrid to avoid truncation of text.
   // Fill grid with desired text strings first.
   // If a column contains no text, DefaultColWidth will be used.
   // Pass [] for ExcludeColumns to process all columns, including Fixed.
   // Columns whose numbers (0-based) are specified in ExcludeColumns will not
   // have their widths adjusted.

 implementation

 uses
   Math; // we need the Max function
   procedure SetOptimalGridCellWidth(sg: TStringGrid;
   ExcludeColumns: TExcludeColumns);

 var
   i : Integer;
   j : Integer;
   max_width : Integer;
 begin
   with sg do
   begin
     // If the grid's Paint method hasn't been called yet,
     // the grid's canvas won't use the right font for TextWidth.
     // (TCustomGrid.Paint normally sets this, under DrawCells.)
     Canvas.Font.Assign(Font);
     for i := 0 to (ColCount - 1) do
     begin
       if i in ExcludeColumns then
         Continue;
       max_width := 0;
       // Search for the maximal Text width of the current column.
       for j := 0 to (RowCount - 1) do
         max_width := Math.Max(max_width,Canvas.TextWidth(Cells[i,j]));
       // The hardcode of 4 is based on twice the offset from the left
       // margin in TStringGrid.DrawCell. GridLineWidth is not relevant.
       if max_width > 0 then
         ColWidths[i] := max_width + 4
       else
         ColWidths[i] := DefaultColWidth;
     end; { for }
   end;
 end;

 end.
(2)实现StringGrid的删除,插入,排序行操作(基本操作啦)//实现删除操作
Procedure GridRemoveColumn(StrGrid: TStringGrid; DelColumn: Integer);
 Var Column: Integer;
 begin
   If DelColumn <= StrGrid.ColCount then
   Begin
     For Column := DelColumn To StrGrid.ColCount-1 do
       StrGrid.Cols[Column-1].Assign(StrGrid.Cols[Column]);
     StrGrid.ColCount := StrGrid.ColCount-1;
   End;
 end;

//实现添加插入操作
 Procedure GridAddColumn(StrGrid: TStringGrid; NewColumn: Integer);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值