Navigation

Delete or clear rows in a TStringGrid

If you ever needed to delete rows from a TStringGrid component you
should have noticed that it does not have a method for deleting or clearing
rows. Here is nice class helper which does that.

Declare a class helper type:

type

TStringGridRowDeletion=classhelperforTStringGrid

public

procedureRemoveRows(RowIndex,RCount:Integer);

procedureClear;

end;

and implement it as follows:

{ TStringGridRowDeletion }

procedureTStringGridRowDeletion.Clear;

var

i:integer;

begin

fori:=0toRowCount-1do

RemoveRows(0,RowCount);

end;

procedureTStringGridRowDeletion.RemoveRows(RowIndex,RCount:Integer);

var

i:Integer;

begin

fori:=RowIndextoRowCount-1do

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 separate unit for it and "use" it
where required. From now on you can delete rows the easy way. E.g.

var

myStringGrid:TStringGrid;

begin

...

myStringGrid.RemoveRows(0,4);

...

end;

Note: the RemoveRows method can be invoked from all
TStringGrid descendants.