Recent

Author Topic: DBGrid multiselect  (Read 2682 times)

teresa

  • Guest
DBGrid multiselect
« on: May 18, 2024, 11:24:51 am »
I have a DBGrid, with multi-select enabled.

I need to know which rows are selected, and short of checking the entire grid row  by row have come up short. With more than 2000 rows, that is time consuming

Is there a more direct way? I see there is the bookmark list, but haven't had any success with it.

Thanks
Teresa

jesusr

  • Sr. Member
  • ****
  • Posts: 496
Re: DBGrid multiselect
« Reply #1 on: May 18, 2024, 08:12:17 pm »
You could use Bookmarklist's enumerator, something like:
Code: Pascal  [Select][+][-]
  1.         for book in theDbGrid.SelectedRows.GetEnumerator([]) do begin
  2.           <do something with the current record>
  3.         end;
  4.  

GetEnumerator takes a set of options:
  • breDisableDataset. Which disables any db gui control, so iterating over selected records is faster
  • breStopOnInvalidBookmark. Which stops iteration if a bookmark is invalid
  • breRestoreCurrent. Which restores the current record after iterating selected records has finished

teresa

  • Guest
Re: DBGrid multiselect
« Reply #2 on: May 19, 2024, 04:36:43 am »
Thanks Jesusr.

I've had no success so far interpreting the byte array (15 bytes) returned for each bookmark.

Is there any documentation on that?

T.

teresa

  • Guest
Re: DBGrid multiselect
« Reply #3 on: May 19, 2024, 07:02:41 am »
Ah, solved. A post from 2017 by GAN showed me how to use the bookmark, and relate it to the dataset.

Thanks all
T.

CharlyTango

  • Jr. Member
  • **
  • Posts: 90
Re: DBGrid multiselect
« Reply #4 on: May 19, 2024, 10:54:33 am »
Could you please post the link, so this thread is linked to the solution ?
Lazarus stable, Win32/64

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: DBGrid multiselect
« Reply #5 on: May 19, 2024, 01:09:53 pm »
Could you please post the link, so this thread is linked to the solution ?
The only related post from GAN that I was able to locate can be found in this thread
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

teresa

  • Guest
Re: DBGrid multiselect
« Reply #6 on: May 20, 2024, 12:01:49 am »

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Re: DBGrid multiselect
« Reply #7 on: May 22, 2024, 05:32:47 am »
I'm trying to extend this to delete multiple rows at once in following way. BeforeDelete event is to delete the record from SQL server before the record is deleted at the TDataSet.

Code: Pascal  [Select][+][-]
  1. procedure TSampleList.DBNavigator1Click(Sender: TObject;
  2.    Button: TDBNavButtonType);
  3. var
  4.    i: Integer;
  5. begin
  6.    if Button = nbDelete then begin
  7.       for i:=0 to dbgSampleList.SelectedRows.Count-1 do
  8.          with dbgSampleList.DataSource.DataSet do
  9.          begin
  10.             GotoBookmark(pointer(dbgSampleList.SelectedRows.Items[i]));
  11.             Delete;
  12.          end;
  13.    end;
  14. end;
  15.  
  16. procedure TSampleList.bdsSampleListBeforeDelete(DataSet: TDataSet);
  17. var
  18.    tclause: string;
  19. begin
  20.    TClause := Format('project_id=''%s'' and respondent_id=%d',
  21.              [pid, bdsSampleList.FieldByName('RID').AsInteger]);
  22.    if not DataAccess.DeleteARecord('SampleList', tclause)
  23.       then bdsSampleList.Cancel;
  24. end;
  25.  

This deletes as I want, but it causes memory leak, as attached in the image. What might have caused this?

 

TinyPortal © 2005-2018