delphi想TStringGrid中添加和删除行的方法  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi想TStringGrid中添加和删除行的方法


{...}
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    {...}
  public
    {...}
  end;

type
  TStringGridHack = class(TStringGrid)
  protected
    procedure DeleteRow(ARow: Longint); reintroduce;
    procedure InsertRow(ARow: Longint);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TStringGridHack.DeleteRow(ARow: Longint);
var
  GemRow: Integer;
begin
  GemRow := Row;
  if RowCount  FixedRows + 1 then
    inherited DeleteRow(ARow)
  else
    Rows[ARow].Clear;
  if GemRow then Row := GemRow;
end;

procedure TStringGridHack.InsertRow(ARow: Longint);
var
  GemRow: Integer;
begin
  GemRow := Row;
  while ARow do Inc(ARow);
  RowCount := RowCount + 1;
  MoveRow(RowCount - 1, ARow);
  Row := GemRow;
  Rows[Row].Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Insert Row, Zeile hinzufügen
  TStringGridHack(StringGrid1).InsertRow(1);
  // Remove Row, Zeile entfernen
  TStringGridHack(StringGrid1).DeleteRow(2);
end;

end.
//该代码片段来自于: http://www.sharejs.com/codes/delphi/8760

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.03267502784729 seconds