Recent

Author Topic: [SOLVED] how to modify data on insert into stringgrid  (Read 2703 times)

glubbish

  • Jr. Member
  • **
  • Posts: 66
[SOLVED] how to modify data on insert into stringgrid
« on: March 04, 2019, 12:56:14 am »
My app has a stringgrid (called data).
I enter values into the grid via ctrl-c ctrl-v
What I want to do is change a column value (comes as date) to a different date format.

I can get this to work with an onselectcell
Code: Pascal  [Select][+][-]
  1. procedure TSharesForm.DataSelectCell(Sender: TObject; aCol, aRow: Integer;
  2.   var CanSelect: Boolean);
  3. var dt: TdateTime;
  4.   s : string;
  5. begin
  6.  if ((acol<>4) and (acol<>7)) then exit;
  7.  dt := ScanDateTime('DD"/"MM"/"YYYY', Data.Cells[acol,arow]);
  8.  Data.Cells[acol,arow] := FormatDateTime('DD MMM YYYY',dt);
  9. end;

But would like it to change when the data is inserted (ctrl-v).
« Last Edit: March 04, 2019, 09:36:54 pm by glubbish »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: how to modify data on insert into stringgrid
« Reply #1 on: March 04, 2019, 01:02:24 am »
Do it in a handler for OnCellProcess, checking that
processtype = cpPaste
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

glubbish

  • Jr. Member
  • **
  • Posts: 66
Re: how to modify data on insert into stringgrid
« Reply #2 on: March 04, 2019, 03:39:17 am »
Thanks lucamar.

I gave it a try but got: EConvertError couldn't match entire pattern string. Input too short at pattern position 1
Code: Pascal  [Select][+][-]
  1. procedure TSharesForm.DataCellProcess(Sender: TObject; aCol, aRow: Integer;
  2.   processType: TCellProcessType; var aValue: string);
  3. var dt: TdateTime;
  4. begin
  5.  if processType <> cpPaste then exit;
  6.  if ((acol<>4) and (acol<>7)) then exit;
  7.  dt := ScanDateTime('DD"/"MM"/"YYYY', Data.Cells[acol,arow]);
  8.  Data.Cells[acol,arow] := FormatDateTime('DD MMM YYYY',dt);
  9. end;
Checking shows that Data.Cells[acol,arow] is null at this point
Thanks Derek


glubbish

  • Jr. Member
  • **
  • Posts: 66
Re: how to modify data on insert into stringgrid
« Reply #3 on: March 04, 2019, 06:28:42 am »
Got this to work.

One solution is:
Code: Pascal  [Select][+][-]
  1. procedure TSharesForm.DataCellProcess(Sender: TObject; aCol, aRow: Integer;
  2.   processType: TCellProcessType; var aValue: string);
  3. var dt: TdateTime;
  4. begin
  5.  if processType <> cpPaste then exit;
  6.  if ((acol<>4) and (acol<>7)) then exit;
  7.  dt := ScanDateTime('DD"/"MM"/"YYYY', aValue);
  8.  aValue := FormatDateTime('DD MMM YYYY',dt);
  9. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: how to modify data on insert into stringgrid
« Reply #4 on: March 04, 2019, 02:07:32 pm »
Yes that's how it has to be done. You can mark the thread as solved now :)

And sorry about the delay but I went to bed shortly after posting and haven't come back until now :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

glubbish

  • Jr. Member
  • **
  • Posts: 66
Re: how to modify data on insert into stringgrid
« Reply #5 on: March 04, 2019, 09:21:09 pm »
I would love to mark solved, but for the life of me, I cannot see how its done.
I could not even find it in the help.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: how to modify data on insert into stringgrid
« Reply #6 on: March 04, 2019, 09:28:22 pm »
It's not in the help because it's just a convention of this forum :)

Simply edit the very frst post and add "[SOLVED]" in front of the "Subject" field.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018