delphitcxgrid

copy entire record and then insert it


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;

Solution

  • 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.