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

本例功能:
1、获取 StringGrid 的行数、列数;
2、给单元赋值.

运行效果图:

学用 TStringGrid [1] - ColCount、RowCount、Cells
//示例代码:
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.

 
 
 
 
 

 

 
  

你可能感兴趣的:(学用 TStringGrid [1] - ColCount、RowCount、Cells)