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

相关阅读 >>

Delphi模拟点击网页中的按钮

isleapyear:返回给定的年份是否是闰年

Delphi ipnumberipv4

Delphi 手机app打开一个安卓系统支持的文件,比如 pdf

Delphi 显示提示信息xxx 是/否

Delphi idhttp最简洁的修改和取得cookie例子

Delphi 简单方法搜索定位treeview项

Delphi leftstr 返回字符串左边指定个数的新字符(串)

行为判断Delphi中窗体最大化与最小化事件

Delphi 的编译指令(1): $define、$undef、$ifdef、$else、$endif

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...