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;
分类:
XE2 与 FireMonkey
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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