[Solved] DBGrid : add button in a cell to start the command "opendocument" lazarus / 16-Databases ---------------------------------------------------------------------- folkeu08 · 2022-08-23 21:15 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 ---------------------------------------------------------------------- tonyw · 2022-08-23 22:15 Quote from: folkeu08 on August 23, 2022, 11:15:19 pmHi 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 · 2022-08-24 08:50 I've attached a simple example of the TDBGrid buttons. Hope that helps. ---------------------------------------------------------------------- folkeu08 · 2022-08-25 08:29 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 · 2022-08-25 08:33 Could be the string field size. ---------------------------------------------------------------------- folkeu08 · 2022-08-25 11:51 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i BufDataset.FieldDefs.Add(fnDocument, ftString, 100);  Now, it's ok Thanks ---------------------------------------------------------------------- folkeu08 · 2022-08-28 16:18 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 ---------------------------------------------------------------------- folkeu08 · 2022-08-28 20:43 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 · 2022-08-28 21:21 Hi, I have find : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin    //Click for view the source document    If CourrierSortant_DBGrid.SelectedColumn.Index = 8 then       begin       if not OpenDocument(TrimAndExpandFilename(Courrier_DataModule.Courri erSortant_SQLQuery.FieldByName('link_source').AsString, Application.Location)) then           begin             ShowMessage('Unable to open document');           end;     end;    //Click for view the pdf document    If CourrierSortant_DBGrid.SelectedColumn.Index = 10 then       begin       if not OpenDocument(TrimAndExpandFilename(Courrier_DataModule.Courr ierSortant_SQLQuery.FieldByName('link_pdf').AsString, Application.Location)) then           begin            ShowMessage('Unable to open document');       end;    end;  end;                     Thanks ---------------------------------------------------------------------- dje · 2022-08-29 04:18 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.