Recent

Author Topic: after LoadFromSpreadsheetFile, cells loose editing capabilities  (Read 230 times)

CapelliC

  • Jr. Member
  • **
  • Posts: 53
after LoadFromSpreadsheetFile, cells loose editing capabilities
« on: September 16, 2020, 05:47:12 pm »
Sorry the (most probably) stupid question, I cannot find the right options to enable cells editing after a file have been loaded.
I have this minimal code, that works like a charm, but I'm unable to change the content *after* the file has been loaded.

procedure TForm1.OpenExcelFileClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    sWorksheetGrid1.LoadFromSpreadsheetFile(OpenDialog1.FileName);
end;

procedure TForm1.SaveAsClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
    sWorksheetGrid1.SaveToSpreadsheetFile(SaveDialog1.FileName);
end;

I have managed to modify the content adding
    a sCellEdit1: TsCellEdit;
and a
    sWorkbookSource1: TsWorkbookSource;
to connect the sWorksheetGrid1 to sCellEdit1, and tried (again) to check/uncheck options/goEditing and AutoEdit, but to change a cell content I must enter the text in sCellEdit1 and press enter, which is strange, since it's required *only* after a file has been loaded.

Could someone drop me an hint ?
TIA, Carlo

wp

  • Hero Member
  • *****
  • Posts: 7521
Re: after LoadFromSpreadsheetFile, cells loose editing capabilities
« Reply #1 on: September 16, 2020, 06:25:07 pm »
You only must add goEditing to the Options of the grid in order to be able to edit a cell. In fact, I don't know exactly what AutoEdit is doing, it is inherited from the ancestor, TCustomGrid.

See the attached demo in which you can load an xlsx file and you can check/uncheck the "read-only" combo to enable/disable editing at any time (before or after loading).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

CapelliC

  • Jr. Member
  • **
  • Posts: 53
Re: after LoadFromSpreadsheetFile, cells loose editing capabilities
« Reply #2 on: September 17, 2020, 09:23:14 am »
Thanks, but the behaviour didn't change.
Don't worry, I will keep the editing bar to enable modification.

wp

  • Hero Member
  • *****
  • Posts: 7521
Re: after LoadFromSpreadsheetFile, cells loose editing capabilities
« Reply #3 on: September 17, 2020, 09:42:31 am »
Then I don't understand the issue. Please post a short compilable project which demonstrates the issue (only .pas, .lfm, .lpi and .lpr files, no compiler-generated files like .exe, .o., ppu etc; all packed to a common .zip which you can upload under "Attachment and other options").
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

CapelliC

  • Jr. Member
  • **
  • Posts: 53
Re: after LoadFromSpreadsheetFile, cells loose editing capabilities
« Reply #4 on: September 17, 2020, 12:05:47 pm »
What I mean is that also the project you provided (edit_grid) doesn't allow editing after loading.
Anyway, here is my test project - where you can change content acting into the edit at top.
I must say I've found anyway your sample code useful, suggesting the syntax required to change by code an option in the grid (I was using the form editor for my test), so I have verified that after loading, forcing the goEditing option  to true didn't solve my problem.

wp

  • Hero Member
  • *****
  • Posts: 7521
Re: after LoadFromSpreadsheetFile, cells loose editing capabilities
« Reply #5 on: September 17, 2020, 12:26:06 pm »
After you clicked the Open button the focus of the form is still on the button. So, when you begin typing the key presses are received by the button, not by the grid. After loading you must click into the grid to move the focus to it. Of course you can do this automatically when you call Worksheetgrid.SetFocus after loading:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.OpenExcelFileClick(Sender: TObject);
  2. begin
  3.   if OpenDialog1.Execute then
  4.     begin
  5.       sWorksheetGrid1.LoadFromSpreadsheetFile(OpenDialog1.FileName);
  6.       sWorksheetGrid1.SetFocus;    // <--------- ADDED
  7.       //sWorksheetGrid1.Options2 := sWorksheetGrid1.Options2 + [goEditing];   // wp: Use "Options", not "Options2" here.
  8.     end;
  9. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018