Recent

Author Topic: [Solved] Copy custom data from existing row to new row in TStringGrid  (Read 1740 times)

heejit

  • Full Member
  • ***
  • Posts: 245
Edit:
I have assign a object to tstringgrid.objects property there is
need to copy selected row to new row.

How do I copy assigned object from existing row and assigned to new row

« Last Edit: October 22, 2018, 10:52:26 am by jshah »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Copy custom data from existing row to new row in TStringGrid
« Reply #1 on: October 21, 2018, 03:04:32 pm »
it's assumed you are using the Copy and paste features of the grid ? if so implement the OnCellProcessEvent which takes
place when you do a copy or paste.

 The ProcessType variable given will indicate which step is talking place so its most likely you will need to create an object
array that will hold the list of objects during the copying step and when the paste step is active you can then refer to the
object list you made and update that objects list of the grid.

 Remember to set a flag that can be tested during the paste to make sure you have a valid object list incase the source of
your paste didn't come from a copy of the grid.

  I am sure there are other ways, that one comes to mind and if I see any other options that maybe better I'll add to this..

The only true wisdom is knowing you know nothing

heejit

  • Full Member
  • ***
  • Posts: 245
Re: Copy custom data from existing row to new row in TStringGrid
« Reply #2 on: October 21, 2018, 03:28:18 pm »
I am not using copy paste method of grid.

There is button when user click on it then selected row
will be added to same grid with all column data with few column data change and
associated custom data.

Problem is custom data is a class and need to copy the class also to new
row and this class also vary depend on what type of data I am displaying in grid.

May be I can create a generic class which will have all data type field then I can write generic procedure
to copy all the field to new class.




jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Copy custom data from existing row to new row in TStringGrid
« Reply #3 on: October 21, 2018, 03:46:38 pm »
Well I guess you already know what needs to be done since you already have a block of code doing that part for you, why
is it a problem to simply copy the Object elements over to the new location after you determine if they apply?

The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Copy custom data from existing row to new row in TStringGrid
« Reply #4 on: October 21, 2018, 05:32:19 pm »
A simple example is attached which you can adapt to your needs.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: Copy custom data from existing row to new row in TStringGrid
« Reply #5 on: October 21, 2018, 06:30:52 pm »
@howardpc : Thanks

I ended up using  fgl unit map type

Code: Pascal  [Select][+][-]
  1. TCellData = specialize TFPGMap<string, variant>;
  2.  
  3. some wrapper function to mange it
  4.     procedure set_udata(ridx: integer; xkey: string; xvalue: variant);
  5.     function get_udata(ridx: integer; xkey: string): Variant;
  6.  
  7. procedure TMyStringGrid.set_udata(ridx:integer; xkey:string; xvalue: variant);
  8. begin
  9.   if Not Assigned(self.Objects[0, ridx]) Then begin
  10.     self.Objects[0, ridx] := TCellData.Create;
  11.     TCellData(self.Objects[0, ridx]).Sorted:=True;
  12.   end;
  13.   TCellData(self.Objects[0, ridx]).AddOrSetData(xkey, xvalue);
  14. end;
  15.  
  16. function TMyStringGrid.get_udata(ridx:integer; xkey:string): Variant;
  17. var
  18.   xidx : integer;
  19. begin
  20.   if Not Assigned(self.Objects[0, ridx]) Then begin
  21.     Result := Nil;
  22.   end else if TCellData(self.Objects[0, ridx]).Find(xkey, xidx) = False Then begin
  23.     Result := Nil;
  24.   end else begin
  25.     Result := TCellData(self.Objects[0, ridx]).KeyData[xkey];
  26.   end;
  27.  
  28. end;
  29.  
  30.  
  31. Copy row as follow
  32. procedure TMySTringGrid.copy_row(from_idx : integer; to_idx : integer);
  33. var
  34.   cidx  : Integer;
  35.   i     : integer;
  36. begin
  37.   for cidx := 0 to self.ColCount -1 do begin
  38.     self.Cells[cidx, to_idx] := self.Cells[cidx, from_idx];
  39.   end;
  40.  
  41.   for i := 0 to TCellData(self.Objects[0, from_idx]).Count - 1  do begin
  42.     self.set_udata(to_idx, TCellData(self.Objects[0, from_idx]).Keys[i], TCellData(self.Objects[0, from_idx]).Data[i]);
  43.   end;
  44. end;
  45.  
  46.  
« Last Edit: October 22, 2018, 10:52:12 am by jshah »

 

TinyPortal © 2005-2018