Ver Mensaje Individual
  #2  
Antiguo 06-03-2020
Avatar de movorack
[movorack] movorack is offline
Miguel A. Valero
 
Registrado: feb 2007
Ubicación: Bogotá - Colombia
Posts: 1.346
Reputación: 22
movorack Va camino a la famamovorack Va camino a la fama
Este código es originario del compañero [gatosoft], depronto te sirva

Código Delphi [-]
  TDatasetHelper = class helper for TDataSet
  Private
  Public
    procedure CopyCurrentRecord(DataSet: TDataset; IgnoreFieldNames: array of string); overload;
    procedure CopyCurrentRecord(DataSet: TDataset); overload;
    procedure CopyData(DataSet: TDataset; IgnoreFieldNames: array of string); overload;
    procedure CopyData(DataSet: TDataset); overload;  
  end;

procedure TDatasetHelper.CopyCurrentRecord(DataSet: TDataset);
begin
  CopyCurrentRecord(DataSet, []);
end;

procedure TDatasetHelper.CopyCurrentRecord(DataSet: TDataset;
  IgnoreFieldNames: array of string);
  function IgnoreField(FieldName: string): Boolean;
    var
      lFieldName : String;
  begin
    Result := False;
    for lFieldName in IgnoreFieldNames do
    begin
      Result := SameText(FieldName.Trim, lFieldName.trim);
      if Result then
        Break;
    end;
  end;
  var
    lField: TField;
begin
  if (not Dataset.Active) or Dataset.IsEmpty then
    Exit;

  if not (Self.State in [dsEdit, dsInsert]) then
    if Self.IsEmpty then
      Self.Append
    else
      Self.Edit;

  for lField in Self.Fields do
  begin
    if IgnoreField(lField.FieldName) then
      Continue;

    if not Assigned(DataSet.FindField(lField.FieldName)) then
      Continue;

    if lField.IsBlob then
    begin
      try
        //Se intenta realizar la copia
        lField.AsBytes := DataSet.FieldByName(lField.FieldName).AsBytes
      except
      end;
    end
    else
    if (lField.Value <> DataSet.FieldByName(lField.FieldName).Value) then
      lField.Value := DataSet.FieldByName(lField.FieldName).Value;
  end;
end;

procedure TDatasetHelper.CopyData(DataSet: TDataset);
begin
  CopyData(DataSet, []);
end;

procedure TDatasetHelper.CopyData(DataSet: TDataset; IgnoreFieldNames: array of string);
  var
    BookMark: TBookmark;
begin
  if (not DataSet.Active) or DataSet.IsEmpty then
    Exit;

  if Self.State in [dsEdit, dsInsert] then
    Self.Cancel;

  BookMark := nil;
  try
    DataSet.DisableControls;
    Self.DisableControls;
    BookMark := DataSet.GetBookmark;

    DataSet.First;
    while not DataSet.Eof do
    begin
      Self.Append;
      CopyCurrentRecord(Dataset, IgnoreFieldNames);
      Self.Post;
      Dataset.Next;
    end;

    Self.First;
  finally
    if Assigned(BookMark) and Dataset.BookmarkValid(BookMark) then
      DataSet.GotoBookmark(BookMark);

    DataSet.EnableControls;
    Self.EnableControls;
  end;
end;
__________________
Buena caza y buen remar... http://mivaler.blogspot.com
Responder Con Cita