Thursday, October 8, 2009

Delete/Clear rows in TStringGrid

If you ever needed to delete rows from a TStringGrid component you should have notice that it does not have a method for deleting/Clearing rows, here is nice class helper which does that, declare a class helper type
type
    TStringGridRowDeletion = class helper for TStringGrid
    public
          procedure RemoveRows(RowIndex, RCount: Integer);
          procedure Clear;
    end;
and the implementation
{ TStringGridRowDeletion }

procedure TStringGridRowDeletion.Clear;
var i: integer;
begin
     for i := 0 to RowCount -1 do
         RemoveRows(0, RowCount);
end;

procedure TStringGridRowDeletion.RemoveRows(RowIndex, RCount: Integer);
var i: Integer;
begin
     for i := RowIndex to RowCount - 1 do
         Rows[i] := Rows[i + RCount];
     RowCount := RowCount -RCount;
end;
now all you have to do is to add this helper in a unit where you need to call RemoveRows or create a utility unit(like most projects have) and add it to uses clause, from now on you can delete rows the easy way
...
var myStringGrid: TStringGrid;
begin
     myStringGrid.RemoveRows(0, 4); 
end;
Note: RemoveRows procedure can be invoked from all TStringGrid descendants.

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)