Recent

Author Topic: [Title change] How to set a checkbox in DBGrid linked to a BookmarkList  (Read 2386 times)

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Hi,

I'm using a BookmarkList to add an additonal column to a DBGrid containing a boolean checkbox with some code I found (see below) snd it works perfect for selecting and deselecting individual rows checkboxes.
However, I'd like to add options (buttons) to select and deselect all checkboxes, but just can't figure out how to access the boolean value for the checkboxes.
I've tried setting up a loop to move through the data set associated with the other values and used
Code: Pascal  [Select][+][-]
  1.     RecList.CurrentRowSelected := True;
but this does not show up in the actual grid. I think I need to access the AState, but just dont know where this is as it doesn't belong to either the grid or the bookmarklist.

Any help would be really appreciated, thanks

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1CellClick(Column: TColumn);
  2. begin
  3.   if Column.Index=1 then
  4.     RecList.CurrentRowSelected := not RecList.CurrentRowSelected;
  5. end;
  6.  
  7. procedure TForm1.FormCreate(Sender: TObject);
  8. begin
  9.   RecList := TBookmarkList.Create(DbGrid1);
  10. end;
  11.  
  12. procedure TForm1.FormDestroy(Sender: TObject);
  13. begin
  14.   RecList.Free;
  15. end;
  16.  
  17. procedure TForm1.DBGrid1UserCheckboxState(sender: TObject; column: TColumn; var AState: TCheckboxState);
  18. begin
  19.   if RecList.CurrentRowSelected then
  20.     AState := cbChecked
  21.   else
  22.     AState := cbUnchecked;
  23. end;
« Last Edit: March 19, 2019, 02:20:11 am by daveinhull »
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: How to access a record in a Bookmarklist
« Reply #1 on: March 15, 2019, 06:49:35 am »
Hi, anyone got any thoughts on this?

Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: How to access a record in a Bookmarklist
« Reply #2 on: March 16, 2019, 02:06:27 am »
It works here:

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.ToggleAll(Ok:boolean);
  2. var
  3.   book: TBookMark;
  4. begin
  5.   FTable.DisableControls;
  6.   book := FTable.GetBookmark;
  7.   try
  8.     FTable.First;
  9.     while not FTable.EOF do begin
  10.       RecList.CurrentRowSelected := Ok;
  11.       FTable.Next;
  12.     end;
  13.   finally
  14.     FTable.GotoBookmark(book);
  15.     FTable.FreeBookmark(book);
  16.     FTable.EnableControls;
  17.   end;
  18. end;
  19.  
  20. procedure TForm1.BtnCheckAll(Sender:TObject);
  21. begin
  22.   ToggleAll(true);
  23. end;
  24. procedure TForm1.BtnUnCheckAll(Sender:TObject);
  25. begin
  26.   ToggleAll(false);
  27. end;
  28.  

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: How to access a record in a Bookmarklist
« Reply #3 on: March 16, 2019, 02:37:26 pm »
Hi jesusr,

Thanks for the suggestion, but it still doesn't set the checkbox to checked or unchecked. It does something because the current record goes to the end of the list.
I'm still thinking I need to some how also do the following using AState to actually check or uncheck the box.
The column is not attached to a record in the table, but the bookmarklist holds the values so somehow I need to replicate the bookmarklist values into the grid checkboxes. When I select an indivdual checkbox, both UserCheckboxState and CellClick are called (see original post).

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1UserCheckboxState(sender: TObject; column: TColumn; var AState: TCheckboxState);
  2. begin
  3.   if RecList.CurrentRowSelected then
  4.     AState := cbChecked
  5.   else
  6.     AState := cbUnchecked;
  7. end;

Any other thoughts?
Thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Hi  anyone got any thoughts on this?
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

jesusr

  • Sr. Member
  • ****
  • Posts: 484
The code I showed you is intended to work 'in addition' to the code you showed in your first msg (which if I'm not wrong corresponds to the sample code in the wikis Grids Reference Page).

If you want specific help and because I cannot reproduce your problem, you can prepare a sample project I will try it here.

 

TinyPortal © 2005-2018