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

37 Visitors Online


 
...seek for a value in a StringGrid and select the right row?
Autor: Schatzl Reinhard
[ Print tip ]  

Tip Rating (10):  
     


/////////////////////////////////////////////////////////////////////////////////////////////////
//Im StringGrid nach Zelleninhalt einer oder mehrerer Spalten suchen und Row Selectieren

function SeekRecord(Grid: TStringGrid; ColOrder: array of Integer;
  StrList: array of string): Boolean;
var
  
I, J: Integer;
begin
  
Result := False;
  for I := 1 to Grid.RowCount - 1 do
  begin
    for 
J := Low(ColOrder) to High(ColOrder) do
    begin
      if 
(Grid.Cells[ColOrder[J], I] = StrList[J]) then
        
Result := True
      else
        
Result := False;
      if Result = False then Break;
    end;
    if Result = True then Break;
  end;

  if Result = True then
    
Grid.Row := I
  else
    
ShowMessage('Datensatz nicht gefunden!' + #13#13 + 'Record not Found!');
end;

//Example
procedure TForm1.Button1Click(Sender: TObject);
begin
  
//Suche Zeile nach Column 0 mit Suchbegriff
  
SeekRecord(Grid, [0], ['Suchbegriff']);
  //Suche Zeile nach Column 0 und 1 mit Suchbegriffen 1 und 2
  
SeekRecord(Grid, [0, 1], ['Suchbegriff1', 'Suchbegriff2']);
  //Suche Zeile nach Column 1, 5 und 2 mit Suchbegriffen 1, 2 und 3
  
SeekRecord(Grid, [1, 5, 2], ['Suchbegriff1', 'Suchbegriff2', 'Suchbegriff3']);
  //usw.
end;

 

Rate this tip:

poor
very good


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