学用 TStringGrid [3] - Cols、Rows

须知: TStringGrid 的每行、每列都是一个 TStrings 对象.

本例功能:
1、分别按列与行修改;
2、分别按列与行读取

运行效果图:

学用 TStringGrid [3] - Cols、Rows

//本例代码:

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;

    Button3: TButton;

    Button4: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



{按列赋值}

procedure TForm1.Button1Click(Sender: TObject);

var

  List: TStrings;

begin

  List := TStringList.Create;

  List.CommaText := 'aaa,bbb,ccc,ddd';

  StringGrid1.Cols[0] := List;

  StringGrid1.Cols[1] := List;

  List.Free;

  (Sender as TButton).Caption := '按列赋值';

end;



{按行赋值}

procedure TForm1.Button2Click(Sender: TObject);

var

  List: TStrings;

begin

  List := TStringList.Create;

  List.CommaText := '111,222,333,444';

  StringGrid1.Rows[0] := List;

  StringGrid1.Rows[1] := List;

  List.Free;

  (Sender as TButton).Caption := '按行赋值';

end;



{按列取值}

procedure TForm1.Button3Click(Sender: TObject);

var

  List: TStrings;

begin

  List := StringGrid1.Cols[StringGrid1.Col]; {当前列}

  ShowMessage(List.Text);

  (Sender as TButton).Caption := '按列取值';

end;



{按行取值}

procedure TForm1.Button4Click(Sender: TObject);

var

  List: TStrings;

begin

  List := StringGrid1.Rows[StringGrid1.Row]; {当前行}

  ShowMessage(List.Text);

  (Sender as TButton).Caption := '按行取值';

end;



end.


 
   

你可能感兴趣的:(String)