学用 TStringGrid [1] - ColCount、RowCount、Cells

本文介绍了一个使用 Delphi 实现的 StringGrid 控件操作示例,展示了如何获取 StringGrid 的行数和列数,并为每个单元格赋值。通过简单的两个按钮点击事件,实现了对网格数据的基本管理。
本例功能:
1、获取 StringGrid 的行数、列数;
2、给单元赋值.

//示例代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{显示列数与行数}
procedure TForm1.Button1Click(Sender: TObject);
var
  cCount,rCount: Integer;
begin
  cCount := StringGrid1.ColCount; {获取总列数}
  rCount := StringGrid1.RowCount; {获取总行数}

  Text := Format('总列数: %d; 总行数: %d', [cCount, rCount]); {显示在标题}
end;

{给每个单元赋值}
procedure TForm1.Button2Click(Sender: TObject);
var
  c,r: Integer;
begin
  for c := 0 to StringGrid1.ColCount - 1 do
    for r := 0 to StringGrid1.RowCount - 1 do
      StringGrid1.Cells[c,r] := Format('%d,%d', [c,r]);
end;

end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值