Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Transfer data from one TDBGRID to another

Hi, i have two TDBGRID's and i want to move data from one to another and backward.
Anyone can help me how to achieve this?

Thx
0
Cherouvim
Asked:
Cherouvim
  • 2
1 Solution
 
kretzschmarCommented:
usual u have two different datasets,
and you copy records from one the other dataset and vice versa

trhe tdbgrid is only a visualizing/editing tool

a small sample
------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, StdCtrls, ExtCtrls, Grids, DBGrids, ADODB;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    ADOTable2: TADOTable;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    Splitter1: TSplitter;
    DBGrid2: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function move_record(ASource, ADest : TCustomADODataset) : boolean;
var i : integer;
begin
  result := false;
  try
    ADest.Insert;
    for i := 0 to ASource.FieldCount - 1 do
      ADest.Fields[i].Value := ASource.Fields[i].Value;
    ADest.Post;
    try
      ASource.Delete;  //try the delete
      result := true;
    except
      ADest.Delete;  //except delete it at dest
    end;
    ASource.Requery([]);
    ADest.Requery([]);
  except
    //nothing
  end;
end;


//use on Buttonclick
procedure TForm1.Button1Click(Sender: TObject);
begin
  if not Move_Record(AdoTable1,AdoTable2) then
    showmessage('There was an Error');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if not Move_Record(AdoTable2,AdoTable1) then
    showmessage('There was an Error');
end;

end.
--------------------------------

see also
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20692990.html

meikl ;-)
0
 
CherouvimAuthor Commented:
Can u send me the source file because i can't work with this.  Thx.
0
 
kretzschmarCommented:
>Can u send me the source file because i can't work with this.  Thx.

of course this is the source, for rebuild just open a new project,
drop the components onto it, create the Button-events,
after this paste the source above in your unit (overwrite the currently app-sceleton)

meikl ;-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now