Jump to content


Photo

Lookup and Locate


  • Please log in to reply
No replies to this topic

#1 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 02 May 2005 - 06:45 PM

Hi fellow coders,

Here two new functions i wrote a couple of days ago. They mimic the locate and lookup functions found in some Borland DB components.

Use like:

CODE
ndx := LocateGrid(aGrid, ['col1','col2'],['value1', 'value2']);


and

CODE
value := LookupGrid(aGrid, ['col1','col2'],['value1', 'value2'], 'resultcol');


And the actual implementation:

CODE
function LocateGrid(const gridview: TNextGrid; colnames, values: array of

 string): Integer;

var

 i                 : Integer;

 j                 : Integer;

 match             : Boolean;

begin

 Result := -1;



 if (High(colnames) <> High(Values)) then exit;



 with gridview do

   for i := 0 to Pred(RowCount) do

     begin

       match := True;

       for j := 0 to High(colnames) do

         if not SameText(CellByName[colnames[j], i].AsString, values[j]) then

           begin

             match := False;

             break;

           end;

       if match then

         begin

           Result := i;

           break;

         end;



     end;

end;


CODE
function LookupGrid(const gridview: TNextGrid; colnames: array of string;

 values: array of variant; return: string): string;

var

 i                 : Integer;

 j                 : Integer;

 match             : Boolean;

begin

 Result := '';



 if (High(colnames) <> High(Values)) then exit;



 with gridview do

   for i := 0 to Pred(RowCount) do

     begin

       match := True;

       for j := 0 to High(colnames) do

         if not SameText(CellByName[colnames[j], i].AsString, VarToStr(values[j])) then

           begin

             match := False;

             break;

           end;

       if match then

         begin

           Result := CellByName[return, i].AsString;

           break;

         end;

     end;

end;

G.W. van der Vegt




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users