Recent

Author Topic: [Solved] DBGrid : add button in a cell to start the command "opendocument"  (Read 784 times)

folkeu08

  • Jr. Member
  • **
  • Posts: 85
Hi boys,
I use a datase with a table who i record the link to a pdf file.
This link apear in a cell of a dbgrid.
I would like to add a button to the cell. By clicking on the button, the pdf viewer linked to the pdf extension should open the link of the file contained in its cell.
It is possible to add a button to each cell of the column of the DBGrid but it seems to me that this button is dedicated to edit the value of the cell. Is it this ? I will not be able to launch the "opendocument" command with the latter.
The solution would be to create a custom button for each cell in the column? Is the idea feasible?
I'm a beginner and not yet experienced enough to write this function.
Does anyone have this piece of code? I couldn't find anything from google
thanks in advance
Folkeu
« Last Edit: August 28, 2022, 11:22:05 pm by folkeu08 »

tonyw

  • Sr. Member
  • ****
  • Posts: 288
    • MWA Software
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #1 on: August 24, 2022, 12:15:58 am »
Hi boys,
I use a datase with a table who i record the link to a pdf file.
This link apear in a cell of a dbgrid.
I would like to add a button to the cell. By clicking on the button, the pdf viewer linked to the pdf extension should open the link of the file contained in its cell.
It is possible to add a button to each cell of the column of the DBGrid but it seems to me that this button is dedicated to edit the value of the cell. Is it this ? I will not be able to launch the "opendocument" command with the latter.
The solution would be to create a custom button for each cell in the column? Is the idea feasible?
I'm a beginner and not yet experienced enough to write this function.
Does anyone have this piece of code? I couldn't find anything from google
thanks in advance
Folkeu
Set the button style to elipses.
Then add a OnEditButtonClick event handler tot he TDBGrid.
In the Event handler use the TDBGrid.SelectedField property to get the field for the current cell. If this is the field holding the path the the document then retrieve the document path and open the document

dje

  • Full Member
  • ***
  • Posts: 124
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #2 on: August 24, 2022, 10:50:33 am »
I've attached a simple example of the TDBGrid buttons. Hope that helps.

folkeu08

  • Jr. Member
  • **
  • Posts: 85
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #3 on: August 25, 2022, 10:29:04 am »
Hi dje,

Thanks for your example.
On my personal PC, the example works fine.
At my work, I also did the test and it does not detect the file. "No File" in the option cell. The pdf extension is well associated with acrobat reader.
I will look for the file that is in the Lazarus installation at: C:\Lazarus_2.2.2\fpc\3.2.2\source\packages\fcl-stl\doc\main.pdf
I put a screenshot.
Thanks
Folkeu

dje

  • Full Member
  • ***
  • Posts: 124
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #4 on: August 25, 2022, 10:33:04 am »
Could be the string field size.

folkeu08

  • Jr. Member
  • **
  • Posts: 85
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #5 on: August 25, 2022, 01:51:53 pm »
Hi,
It's the same result with a white page of word recorded in pdf (138Ko) and a more larger pdf (3Mo).

I've find the little problem :
I've modified the longeur of charactere of fnDocument to 100 (origin 30).
Code: Pascal  [Select][+][-]
  1. BufDataset.FieldDefs.Add(fnDocument, ftString, 100);  
Now, it's ok
Thanks
« Last Edit: August 25, 2022, 02:18:40 pm by folkeu08 »

folkeu08

  • Jr. Member
  • **
  • Posts: 85
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #6 on: August 28, 2022, 06:18:21 pm »
Hi Dje,
I took your source to transcribe it in my application.
I already found a PB with the RXdbGrid I'm using, the buttons don't show up.
I went with a DBGrid and they show up fine with the text inside.
When I click the button, nothing happens.
Should I add the procedure "procedure TFormMain.DBGridEditButtonClick(ADBGrid: TObject);"
How to not make the file selection button appear in the file link cell?
How to attach this procedure to the click on the button?
I extracted my source which corresponds to reality. Just add the "sqlite3.dll" dll to the directory to link the database to the exe.
Folkeu
« Last Edit: August 28, 2022, 08:54:57 pm by folkeu08 »

folkeu08

  • Jr. Member
  • **
  • Posts: 85
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #7 on: August 28, 2022, 10:43:54 pm »
Hi Dje,
I managed to achieve the operation as I wanted from your source.
In my source I save source file path and pdf file path.
In the "DBGridEditButtonClick" procedure, I would like to identify the column clicked to open the correct file, either the source or the pdf.
How to do ?
Thanks
Folkeu

folkeu08

  • Jr. Member
  • **
  • Posts: 85
Re: DBGrid : add button in a cell to start the command "opendocument"
« Reply #8 on: August 28, 2022, 11:21:16 pm »
Hi,
I have find :
Code: Pascal  [Select][+][-]
  1. begin
  2.     //Click for view the source document
  3.     If CourrierSortant_DBGrid.SelectedColumn.Index = 8 then
  4.        begin
  5.          if not OpenDocument(TrimAndExpandFilename(Courrier_DataModule.CourrierSortant_SQLQuery.FieldByName('link_source').AsString, Application.Location)) then
  6.            begin
  7.              ShowMessage('Unable to open document');
  8.            end;
  9.      end;
  10.     //Click for view the pdf document
  11.     If CourrierSortant_DBGrid.SelectedColumn.Index = 10 then
  12.        begin
  13.          if not OpenDocument(TrimAndExpandFilename(Courrier_DataModule.CourrierSortant_SQLQuery.FieldByName('link_pdf').AsString, Application.Location)) then
  14.            begin
  15.             ShowMessage('Unable to open document');
  16.        end;
  17.     end;
  18.   end;                      
  19.  
Thanks

dje

  • Full Member
  • ***
  • Posts: 124
Excellent. My only advice is to try to use DBGrid.SelectedColumn, DBGrid.SelectedField and notice TField.OnGetText sends the field in question as Sender.
I also advice you to define all fieldname as constants. ie: fnLinkSource = ''link_source'

I can see OnGetText is identical for both 'link_source' & 'link_pdf', and the field in question is Sender, so why not just use one method for both?

Note: Column indexes are not a robust way to identify a column since columns can be inserted or moved.

The last thing you want is for code to break when table structures are modified. You can also manually add TDBGrid columns which arn't associated to a field, which means you can add push buttons for records without adding a 'dummy' field.

Just keep an eye on issues that can break code, or situations where you cant edit the table structure. Imagine working on a table for a company that refuses you to add two dummy fields. What do you do? Or, if you are working on a table were the company wants different ordering of columns , or some columns to be readonly or hidden. Maybe they store time as 24 hour, but want users to be able to enter roster times of 1:00 to mean 13:00, since no one starts work at 1:00? ie: shifting the 12 hour boundary. That is all done via OnGetText/OnSetText

The solutions are there, just keep to the wise coding styles. and yep, column indexes are bad.
« Last Edit: August 29, 2022, 06:28:20 am by dje »

 

TinyPortal © 2005-2018