Mega Code Archive

 
Categories / Delphi / VCL
 

How to InsertDelete a row in a TStringGrid

Title: How to Insert/Delete a row in a 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.