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