Board index » delphi » How to duplicate a record in a DBGrid ?

How to duplicate a record in a DBGrid ?

Hi all,
When I press the INS key on a DBGrid, it simply inserts an empty record.
is there any way to duplicate the previous record, so the new inserted
record looks like the previous one. (Hope it is clear)

Thanks in advance
YC

 

Re:How to duplicate a record in a DBGrid ?


In article <3ECB7AE5.2070...@aaa.com>, a...@aaa.com says...

Quote
> Hi all,
> When I press the INS key on a DBGrid, it simply inserts an empty record.
> is there any way to duplicate the previous record, so the new inserted
> record looks like the previous one. (Hope it is clear)

In your Dataset's BeforeInsert event, gather the values from the current record
and in the AfterInsert, set the values of the new record to those that you
gathered.

Re:How to duplicate a record in a DBGrid ?


Thanks for the response,
but I don't know how to do it.
Quote
Eddie Shipman wrote:
> In article <3ECB7AE5.2070...@aaa.com>, a...@aaa.com says...

>>Hi all,
>>When I press the INS key on a DBGrid, it simply inserts an empty record.
>>is there any way to duplicate the previous record, so the new inserted
>>record looks like the previous one. (Hope it is clear)

> In your Dataset's BeforeInsert event, gather the values from the current record
> and in the AfterInsert, set the values of the new record to those that you
> gathered.

Re:How to duplicate a record in a DBGrid ?


In article <3ECB7F36.6010...@aaa.com>, a...@aaa.com says...

Quote
> Thanks for the response,
> but I don't know how to do it.

> Eddie Shipman wrote:
> > In article <3ECB7AE5.2070...@aaa.com>, a...@aaa.com says...

> >>Hi all,
> >>When I press the INS key on a DBGrid, it simply inserts an empty record.
> >>is there any way to duplicate the previous record, so the new inserted
> >>record looks like the previous one. (Hope it is clear)

> > In your Dataset's BeforeInsert event, gather the values from the current record
> > and in the AfterInsert, set the values of the new record to those that you
> > gathered.

I will post a sample in the attachmetns group later today showing you how.
Look for the subject "Scatter/Gather".

Re:How to duplicate a record in a DBGrid ?


In article <MPG.19353e30ddf68b1b989...@forums.borland.com>,
eshipman_remove_the_word_...@flyaustin.rr.com says...

Quote
> In article <3ECB7F36.6010...@aaa.com>, a...@aaa.com says...
> > Thanks for the response,
> > but I don't know how to do it.

> > Eddie Shipman wrote:
> > > In article <3ECB7AE5.2070...@aaa.com>, a...@aaa.com says...

> > >>Hi all,
> > >>When I press the INS key on a DBGrid, it simply inserts an empty record.
> > >>is there any way to duplicate the previous record, so the new inserted
> > >>record looks like the previous one. (Hope it is clear)

> > > In your Dataset's BeforeInsert event, gather the values from the current record
> > > and in the AfterInsert, set the values of the new record to those that you
> > > gathered.

> I will post a sample in the attachmetns group later today showing you how.
> Look for the subject "Scatter/Gather".

I decided to just post the code...no need to check the attachments group.

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    DBGrid1: TDBGrid;
    procedure Table1BeforeInsert(DataSet: TDataSet);
    procedure Table1AfterInsert(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
    AFieldValues: Variant;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure Scatter(ADataset: TDataset; var AList: Variant);
var
  i: Integer;
begin
  // read DataSet values into array
  for i := 0 to (ADataSet.Fieldcount-1) do
  begin
    AList[i] := ADataSet.Fields[i].Value;
  end;
end;

procedure Gather(ADataset: TDataset; AList: Variant);
var
  i: Integer;
begin
  for i := 0 to ADataset.FieldCount-1 do
    ADataset.Fields[i].Value := AList[i];
end;

procedure TForm1.Table1BeforeInsert(DataSet: TDataSet);
begin
  AFieldValues := VarArrayCreate([0,DataSet.Fieldcount-1],VarVariant);
  Scatter(Table1, AFieldValues);
end;

procedure TForm1.Table1AfterInsert(DataSet: TDataSet);
begin
  Gather(Table1, AFieldValues);
  AFieldValues := varNull;
end;

Re:How to duplicate a record in a DBGrid ?


Thanks Eddie, great help.
Regards
YC
Quote
Eddie Shipman wrote:
> I decided to just post the code...no need to check the attachments group.

> type
>   TForm1 = class(TForm)
>     DataSource1: TDataSource;
>     Table1: TTable;
>     DBGrid1: TDBGrid;
>     procedure Table1BeforeInsert(DataSet: TDataSet);
>     procedure Table1AfterInsert(DataSet: TDataSet);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>     AFieldValues: Variant;
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure Scatter(ADataset: TDataset; var AList: Variant);
> var
>   i: Integer;
> begin
>   // read DataSet values into array
>   for i := 0 to (ADataSet.Fieldcount-1) do
>   begin
>     AList[i] := ADataSet.Fields[i].Value;
>   end;
> end;

> procedure Gather(ADataset: TDataset; AList: Variant);
> var
>   i: Integer;
> begin
>   for i := 0 to ADataset.FieldCount-1 do
>     ADataset.Fields[i].Value := AList[i];
> end;

> procedure TForm1.Table1BeforeInsert(DataSet: TDataSet);
> begin
>   AFieldValues := VarArrayCreate([0,DataSet.Fieldcount-1],VarVariant);
>   Scatter(Table1, AFieldValues);
> end;

> procedure TForm1.Table1AfterInsert(DataSet: TDataSet);
> begin
>   Gather(Table1, AFieldValues);
>   AFieldValues := varNull;
> end;

Other Threads