RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TDataSet.Edit Method

Enables editing of data in the dataset.

Pascal
procedure Edit;
C++
__fastcall Edit();

Call Edit to permit editing of the active record in a dataset. Edit determines the current state of the dataset. If the dataset is empty, Edit calls Insert. Otherwise Edit  

Calls CheckBrowseMode to post any pending changes to a prior record if necessary. 

Checks the CanModify property and raises an exception if the dataset can't be edited. 

Calls the BeforeEdit event handler. 

Retrieves the record. 

Puts the dataset into dsEdit state, enabling the application or user to modify fields in the record. 

Broadcasts the state change to associated controls. 

Calls the AfterEdit event handler.  

C++ Examples: 

 

/*
The following example copies the data in the Notes field of
Table1 or SQLDataSet1 to the Remarks field of ClientDataSet1.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TBlobType blobType = Table1Notes->BlobType;
  TStream *Stream1 = new TBlobStream(Table1Notes, bmRead);
  try
  {
    CDS2->Edit();
    // here’s a different way to create a blob stream }
    TStream *Stream2 = CDS2->CreateBlobStream(CDS2->FieldByName("Remarks"), bmReadWrite);
    try
    {
      Stream2->CopyFrom(Stream1, Stream1->Size);
//    CDS2.Post;
//    CDS2.Active := True;
    }
    __finally
    {
      delete Stream2;
    }
  }
  __finally
  {
    delete Stream1;
  }
};
/*
This example uses a button to copy the value of a field in
the previous record into the corresponding field in the
current record.
*/
/*
To ensure that the button is disabled when there is no
previous record, the OnDataChange event of the DataSource
detects when the user moves to the beginning of file (BOF
property becomes true), and disables the button.  Detection
occurs on scrolling and editing, not selection with the mouse.
*/
void __fastcall TForm1::DS2DataChange(TObject *Sender, TField *Field)
{
  if (Customers->Bof)
    Button2->Enabled = False;
  else
    Button2->Enabled = True;
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  TBookmark SavePlace;
  Variant PrevValue;

  // get a bookmark so that we can return to the same record
  SavePlace = Customers->GetBookmark();
  Customers->FindPrior();// move to prior record
  PrevValue = Customers->FindField("Field1")->Value; // get the value
  // Move back to the bookmark
  // this may not be the next record anymore
  // if something else is changing the dataset asynchronously
  Customers->GotoBookmark(SavePlace);
  Customers->Edit();
  Customers->FindField("Field1")->Value = PrevValue; // set the value
  Customers->FreeBookmark(SavePlace);
}

 

Delphi Examples: 

{
This example adds an entry to a memo with a message
when an AfterEdit event occurs and when a BeforeEdit event
occurs.  Click on Run SQL to populate the DBGrid.  Then
select a cell to edit and click the Edit current cell
button, or just start editing the cell to invoke the
BeforeEdit and AfterEdit events.
} 
procedure TForm1.ClientDataSet1AfterEdit(DataSet: TDataSet);
begin
   Memo2.Lines.Add('After editing record' + IntToStr(DataSet.RecNo));
end;

procedure TForm1.ClientDataSet1BeforeEdit(DataSet: TDataSet);
begin
   Memo2.Lines.Add('Before editing record' + IntToStr(DataSet.RecNo));
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  ClientDataSet1.Edit;
end;
{
The following example copies the data in the Notes field of
Table1 or SQLDataSet1 to the Remarks field of ClientDataSet1.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  Stream1: TBlobStream;
  Stream2: TStream;
  blobType : TBlobType;
begin
  blobType := Table1Notes.BlobType;
  Stream1 := TBlobStream.Create(Table1Notes, bmRead);
  try
    CDS2.Edit;
    { here’s a different way to create a blob stream }
    Stream2 := CDS2.CreateBlobStream(CDS2.FieldByName('Remarks'), bmReadWrite);
    try
      Stream2.CopyFrom(Stream1, Stream1.Size);
//    CDS2.Post;
//    CDS2.Active := True;
    finally
      Stream2.Free;
    end;
  finally
    Stream1.Free;
  end;
end;
{
This example uses a button to copy the value of a field in
the previous record into the corresponding field in the
current record.
}
procedure TForm1.Button1Click(Sender: TObject);
var
   SavePlace: TBookmark;
   PrevValue: Variant;
begin
   with Customers do
   begin
    { get a bookmark so that we can return to the same record }
    SavePlace := GetBookmark;
    try
      { move to prior record}
      FindPrior; 
      { get the value }
      PrevValue := FindField('Field2').Value;
      {Move back to the bookmark
      this may not be the next record anymore 
      if something else is changing the dataset asynchronously }
      GotoBookmark(SavePlace);
      { Set the value }
      Edit;
      FindField('Field2').Value := PrevValue;
      { Free the bookmark }
    finally
      FreeBookmark(SavePlace);
    end;
  end;
end;

{
To ensure that the button is disabled when there is no
previous record, the OnDataChange event of the DataSource
detects when the user moves to the beginning of file (BOF
property becomes true), and disables the button.  Detection
occurs on scrolling and editing, not selection with the mouse.
} 
procedure TForm1.DS2DataChange(Sender: TObject; Field: TField);
begin
  if Customers.Bof then
    Button1.Enabled := False
  else
    Button1.Enabled := True;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!