I want to list some values (ID-s in this case) of the selected rows of a TDBGrid in a TEdit control.
I've tried AfterScroll event, to catch the event after(!) a selection, but it doesn't work if I use the mouse.
If I click on a row with mouse, it doesn't appear in the TDBGrid.SelectedRows collection, only after the next click/selection. If I do the selection with keyboard, everything works fine.
Do you have any idea, how to solve this?
Simplified code of my solution:
procedure TForm1.ClientDataSet1AfterScroll(DataSet: TDataSet);
begin
edtIDs.Text := string.Join(',', GetSelectedIDs().ToArray) ;
end;
function TForm1.GetSelectedIDs() : TList<string>;
var
i: Integer;
ds: TDataSet;
bmOrig: TBookmark;
begin
FSelectedIDs.Clear();
ds := DBGrid1.DataSource.DataSet;
bmOrig := ds.GetBookmark();
ds.AfterScroll := nil; //switch off AfterScroll event
try
if DBGrid1.SelectedRows.Count > 0 then begin
for i := 0 to DBGrid1.SelectedRows.Count - 1 do begin
ds.GotoBookmark(DBGrid1.SelectedRows.Items[i]);
FSelectedIDs.Add(ds.FindField('ID').AsString);
end;
ds.GotoBookmark(bmOrig);
end;
finally
ds.AfterScroll := ClientDataSet1AfterScroll; //switch on AfterScroll event
ds.FreeBookmark(bmOrig);
end;
Result := FSelectedIDs;
end;
for
loop by calls tods.DisableControls
&ds.EnableControls
to avoid unneccessary screen updates. – MartynA