0

This is a very short description how I enter the data into the database.

procedure TMain_Form.AdvGlowButton1Click(Sender: TObject);
begin
MYQUERY.Close;
MYQUERY.SQL.Clear;
MYQUERY.SQL.Add('INSERT INTO MYTABLE (FOR_DATE,SOMETEXT) VALUES(:a1,:a2)');
MYQUERY.Params.ParamByName('a1').asDate :=PlannerCalendar1.Date;
MYQUERY.Params.ParamByName('a2').Value :=cxMemo1.Lines.Text ;
MYQUERY.ExecSQL;
end;

This works ok.Data gets inserted.It is shown in the cxGrid1.

Now, what I would like to do is to copy the entire record from this cxGrid1 (only the selected record) and paste (insert it) to a different date,the one selected selected in the PlannerCalendar1.

I have the cxgridpopup menu implemented with the 'send to the selected date' option but I dont know how to copy the underlying data from the cxGrid1.

The objective is to select a date in the calendar then go to the cxGrid1, select a record, right click on it and (using the popup menuoption) and then insert the entire selected record to the newly selected date. Basically cloning the record but to the different date. How can I do this ?

(PlannerCalendar1 is a TMS component)

edit : found some info here https://www.devexpress.com/Support/Center/Question/Details/A302 but not sure how it fits my case.

EDIT : I tried this way and it will not work :

 Procedure CopyTableRecord(View: TcxGridTableView; IndexToCopy : Integer);
var i:integer;
PlannerCalendar1:TPlannerCalendar;
Begin
  View.DataController.Insert;
  For I := 0 To Pred(View.ColumnCount) Do
    View.Columns[I].EditValue := view.DataController.Values[1, I];
  View.Columns[1].EditValue := PlannerCalendar1.Date;
  View.DataController.Post(True);
End;

procedure TMain_Form.Sendto1Click(Sender: TObject);
begin
CopyTableRecord(cxGrid1DBTableView1, cxGrid1DBTableView1.DataController.FocusedRecordIndex);
end;
user763539
  • 3,509
  • 6
  • 44
  • 103

3 Answers3

1

I would separate between data and view. So why not just walk the same path as on inserting a new record?

procedure TMain_Form.Sendto1Click(Sender: TObject);
begin
  MYQUERY.Close;
  MYQUERY.SQL.Clear;
  MYQUERY.SQL.Add('INSERT INTO MYTABLE (FOR_DATE, SOMETEXT) SELECT :a1, SOMETEXT FROM MYTABLE WHERE FOR_DATE = :a2');
  MYQUERY.Params.ParamByName('a1').asDate :=PlannerCalendar1.Date;
  MYQUERY.Params.ParamByName('a2').asDate := cxGrid1DBTableView1.DataController.Values[cxGrid1DBTableView1.DataController.FocusedRecordIndex,1];
  MYQUERY.ExecSQL;
end;

Note: This solution should work unless there are more than one record for a specific date. I presumed, there are not. If there are, you should not select by date but by your primary key column.

René Hoffmann
  • 2,766
  • 2
  • 20
  • 43
  • Uhhh rene, you are right. i have been blind. As a matter of fact all I need is an update query for the date and pass :a2 , the primary key like you suggested. Thank you! – user763539 Sep 04 '15 at 21:36
0

You can clone a record by using the DataController and Columns property of the cxGridTableView you are using like this :-

Procedure CopyTableRecord(View: TcxGridTableView; IndexToCopy : Integer);
Begin
  View.DataController.Insert;
  For I := 0 To Pred(View.ColumnCount) Do
    View.Columns[I].EditValue := View.DataController.Values[IndexToCopy, I];
  // Modify your Date field here...
  View.DataController.Post(True);
End;

Pass the FocusedRecordIndex of the grid view to the above routine.

Andy_D
  • 2,340
  • 16
  • 21
  • Andy, thank you for your reply. Not sure however what you mean by "Modify your date here" . How do I do that ? – user763539 Sep 03 '15 at 07:58
  • Use `View.Columns[].EditValue := ;` – Andy_D Sep 03 '15 at 08:00
  • See the last edit + dont understand this "Pass the FocusedRecordIndex of the grid view to the above routine." – user763539 Sep 03 '15 at 08:07
  • The code you posted is different to my answer. You need to look at the code, understand it and try and work out what you are doing wrong. Hint : The value of `i` is undefined when you use it in the first line of code. – Andy_D Sep 03 '15 at 08:12
  • Thank you Andy_D ...much obliged. But still not sure what you meant by "Pass the FocusedRecordIndex of the grid view to the above routine." :) – user763539 Sep 03 '15 at 08:29
  • @user763539 It is not that hard to look up *FocusedRecordIndex* in the documentation.... – Jan Doggen Sep 03 '15 at 08:32
  • The `DataController` has a `FocusedRecordIndex` property that returns the index of the currently focused record (i.e. the record you are on). So you would call `CopyTableRecord(cxGrid1DBTableView1, cxGrid1DBTableView1.DataController.FocusedRecordIndex);` – Andy_D Sep 03 '15 at 08:33
  • Check that FocusedRecordIndex <> -1. I my answer did not work, why accept it? Or was it working and now you've changed it it's not working? – Andy_D Sep 03 '15 at 11:59
  • At first I had the code on popup menuitem click. It worked OK but failed if there was more than one record in the grid.It definetely needed a focusedrow index. Then I realised yours was the procedure. So I wrote the code as seen in my above edit but the thing crashes when I try to copy the record. What am I doing wrong ? – user763539 Sep 03 '15 at 12:07
  • calling : for i:=0 to cxGrid1DBTableView1.DataController.FocusedRecordIndex -1 do does not help either.... – user763539 Sep 03 '15 at 12:27
  • You need to be explicit. "Crashes" is of no help to anyone. If you look carefully, the code you edited in your question is not the same as the code I posted in my answer. Randomly changing code you don't understand is not the way forward.If you need help, provide as much information as possible. Delphi has a rather good debugger - use it. – Andy_D Sep 03 '15 at 12:44
  • I get : First chance exception at $779BC42D. Exception class EACRException with message 'Invalid PageNo = -1 - Native error: 10471'. Process HT.exe (524) This seems accuracer issue (ACRDiskEngine) : Debugger shows : function TACRDiskPageManager.GetPageOffset(PageNo: TACRPageNo): Int64; begin if (PageNo = INVALID_PAGE_NO) then raise EACRException.Create(10471, ErrorLInvalidPageNo, [PageNo]); Result := Int64(FOffsetToFirstPage) + Int64(PageNo) * Int64(FPageSize); end; // G – user763539 Sep 03 '15 at 18:05
  • very sloppy code...no wonder its mistake.First of all, Aview variable must be declared first :var AView: TcxGridTableView; – user3181689 Sep 04 '15 at 07:04
0

procedure TFrm_Ana.cxButton69Click(Sender: TObject); var degiskenDizimiz:Array of Variant; i:Integer; begin

 SetLength(degiskenDizimiz,dataset.FieldCount);


for i := 0 to dataset.FieldCount-1 do
begin

  degiskenDizimiz[i]:= dataset.Fields[i].Value;


end;

dataset.Insert;

for i := 1 to dataset.FieldCount-1 do // start 1 because 0 possible index field autoincremantal begin dataset.Fields[i].Value:=degiskenDizimiz[i]; end; dataset.Post; end;