随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


TStringGrid、TGrid 都是从 TCustomGrid 继承; 区别有:
1、它们的列对象分别是: TStringColumn、TColumn;
2、TStringGrid 比 TGrid 多出了 Cells[] 属性.

因为 TGrid 没有 Cells[] 属性, 暂时不方便使用; 我尝试取其当前单元值时竟然用了这样的代码:
(Grid1.Columns[Grid1.ColumnIndex].CellControlByRow(Grid1.Selected) as TTextCell).Text

TStringGrid 测试:

{ 设计时放好 StringGrid1, 运行时填充数据 }
procedure TForm1.FormCreate(Sender: TObject);
var
  i,c,r: Integer;
begin
  StringGrid1.AlternatingRowBackground := True;
  StringGrid1.UseSmallScrollBars := True;
  for i := 0 to 5 do //从设计时添加列比这方便
  begin
    with TStringColumn.Create(Self) do
    begin
      Parent := StringGrid1;
      Width := StringGrid1.ClientWidth / 6;
    end;
  end;
  StringGrid1.RowCount := 20;
  for c := 0 to StringGrid1.ColumnCount - 1 do
    for r := 0 to StringGrid1.RowCount - 1 do
      StringGrid1.Cells[c, r] := Format('%d,%d', [c, r]);
end;

{ 取当前单元值 }
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(StringGrid1.Cells[StringGrid1.ColumnIndex, StringGrid1.Selected]);
end;


成员概览:

{ TCustomGrid }
public
  constructor Create(...); override;        //
  destructor Destroy; override;             //
  function ColumnByIndex(...): TColumn;     //根据索引获取列对象
  function ColumnByPoint(...): TColumn;     //根据位置获取列对象
  function RowByPoint(...): Integer;        //根据位置获取行号
  procedure AddObject(...); override;       //
  property TopRow: Integer ...;             //获取可见的首行的行号
  property VisibleRows: Integer ...;        //获取可见的行总数
  property ColumnCount: Integer ...;        //列数(也是只读)
  property ColumnIndex: Integer ...;        //获取或设置列索引
  property Columns[Index: Integer]: TColumn ...; //以数组索引的方式获取列对象
  property RowCount: Integer ...;           //行数(可读写)
  property Selected: Integer ...;           //当前行号
  property OnGetValue: TOnGetValue ...;     //取值时
  property OnSetValue: TOnSetValue ...;     //赋值时
published
  property StyleLookup;                     //
  property AlternatingRowBackground: Boolean ...; //是否使用交替背景; 默认 False
  property CanFocus default True;           //
  property DisableFocusEffect default True; //是否取消焦点特效
  property RowHeight: Single ...;           //行高
  property ShowSelectedCell: Boolean ...;   //是否呈现单元选择效果; 默认 True
  property ShowVertLines: Boolean ...;      //是否显示竖格线
  property ShowHorzLines: Boolean ...;      //是否显示横格线
  property ShowHeader: Boolean ...;         //是否显示表格头
  property ReadOnly: Boolean ...;           //是否只读; 默认 False
  property TabOrder;                        //
  property OnEdititingDone: TOnEdititingDone ...; //输入时
end;

{ TGrid }
TGrid = class(TCustomGrid)
published
  property RowCount;   //
  property OnGetValue; //
  property OnSetValue; //
end;

{ TStringGrid }
public
  property Cells[ACol, ARow: Integer]: string ...; //
published
  property RowCount;   //
end;

posted on   万一  阅读(11631)  评论(21编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2009-10-09 使用泛型的 TArray 从动态数组中查找指定元素
2009-10-09 使用泛型的 TArray 为动态数组排序
2009-10-09 SendTextMessage 等方便的消息发送函数
2009-10-09 WindowsAPI: MulDiv


点击右上角即可分享
微信分享提示