Another piece of code born today. A function to lookup a value from a column based on a value in another column. I use it to lookup values from a key=value list presented in a 2 column gridview.
Usage:
CODE
value:=LookupValue(GridView2, 'ParameterNames', 'aKey', 'ParameterValues');
It looks up the value in the 'ParameterValues' column in the row where the value in the 'ParameterNames column' is 'akey'.
Note: the following should also work:
CODE
value:=LookupValue(GridView2, 0 'aKey', 1);
Source:
CODE
function LookupValue(GridView: TGridView; lookupcolumnname, lookupvalue, valuecolumnname: OleVariant): OleVariant;
var
i : Integer;
begin
Result := '';
with GridView do
for i := 0 to Pred(RowCount) do
if CompareText(CellByName[lookupcolumnname, i].AsString, VarToStr(lookupvalue)) = 0 then
begin
Result := CellByName[valuecolumnname, i].AsString;
Break;
end;
end;
var
i : Integer;
begin
Result := '';
with GridView do
for i := 0 to Pred(RowCount) do
if CompareText(CellByName[lookupcolumnname, i].AsString, VarToStr(lookupvalue)) = 0 then
begin
Result := CellByName[valuecolumnname, i].AsString;
Break;
end;
end;











