whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

33 Visitors Online


 
...implement a filter mechanism for a stringgrid?
Autor: Schatzl Reinhard
[ Print tip ]  

Tip Rating (9):  
     


// 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;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners