Hi there everyone,
I have a stringgrid with ID and Names in the first 2 columns. I have a combobox on top of it where I allow the user to search by ID or by Name (actually all searches are done by ID because the ID corresponding to the name is retrieved from the combobox before starting the search).
When the ID is found in the grid, the corresponding row is highlighted.
The problem is the stringgrid is starting to fill up and sometimes the highlighted row is off-screen and can only be located by scrolling up or down in the grid.
How can I set it up so that the stringgrid
jumps/scrolls to highlighted row immediately after a matching ID is found?
My code is shown below:
// Searches for the name or ID of a person in a stringgrid row
//
// ID and Names are in the first 2 columns (ID - column 0; Names - column 1)
//
// Search is done on column zero (the ID column)
//
procedure TfrmMain.spedLookupClick(Sender: TObject);
var
intID, intCount: integer;
strParam: string;
iFound: boolean;
begin
// Ensure that the Lookup combobox is not empty
if (Trim(cboLookup.Text) <> EmptyStr) then
begin
//
iFound := False;
//
if TryStrToInt(Trim(cboLookup.Text), intID) then
strParam := IntToStr(intID)
else
strParam := IntToStr(Integer(cboLookup.Items.Objects[cboLookup.ItemIndex]));
//
case PageControl1.ActivePage.Caption of
'Services':
begin
//
for intCount := 0 to sgrdServices.RowCount - 1 do
begin
// Search down the ID column
if sgrdServices.Cells[0, intCount] = strParam then
begin
iFound := True;
sgrdServices.Row := intCount; // select the row where the name or ID was found
Break;
end
end;
end; // 'Services'
//
if not iFound then
MessageDlg(strIDNomIntrouvable, mtWarning, [mbOK], 0);
end; // if (Trim(cboLookup.Text) <> EmptyStr) then
end;
Thanks for your contributions
JD