...implement a filter mechanism for a stringgrid?
Author: Schatzl Reinhard
// Filter Liste zum sichern des Gridinhalt
// save the original content
var
FilterList : TStringList;
// Filter setzen
// set the filter
procedure TForm1.SetFilter(ACol:Integer;Exp:String);
var
I,Counter:Integer;
begin
FilterList:=TStringList.Create;
With Grid do
begin
//Filterliste mit Gridinhalt füllen
For I := FixedRows To RowCount - 1 Do
FilterList.Add(Rows[I].Text);
//Grid filtern
Counter:=FixedRows;
For I := FixedRows To RowCount - 1 Do
Begin
If Cells[ACol,I] <> Exp Then
Begin
Rows[I].Clear;
end
Else
begin
If Counter <> I Then
Begin
Rows[Counter].Assign(Rows[I]);
Rows[I].Clear;
End;
Inc(Counter);
End;
End;
RowCount:=Counter;
End;
end;
// Gridinhalt wiederherstellen
// restore the original content
procedure TForm1.RestoreFilter;
var
I:Integer;
begin
With Grid do
begin
RowCount:=FixedRows+FilterList.Count;
For I:=0 To FilterList.Count - 1 Do
Rows[FixedRows+I].Text := FilterList.Strings[I];
End;
FilterList.Free;
end;
//Example
procedure TForm1.BtnSetFilterClick(Sender: TObject);
begin
//Grid nach Zellinhalt der markierten Zelle filtern
SetFilter(Grid.Col,Grid.Cells[Grid.Col,Grid.Row]);
end;
procedure TForm1.BtnRestoreFilterClick(Sender: TObject);
begin
RestoreFilter;
end;
printed from
www.swissdelphicenter.ch
developers knowledge base