呵呵,我今天专门做了测试,达到你说的要求,现在把我做的描述一下。
三个AdoTable,一个AdoConnection,两个DataSource,两个DBGrid,
(昨天我说的主要是没经过实践,所以有一些小情况没考虑到,思路是一样的)
测试连接的数据库是Access,里面建了三个表,分别对应与ADOTable1,ADOTable2,
ADOTable3,2和3是空记录的(如果你想采用这种做法,你可以用两个临时表
来代替,我为了尽快实现功能,没继续复杂下去)。
现在说重点,昨天没实践的时候说是在BeforeScroll事件里写代码,
这是不行的,要在AfterScroll事件里写才行。另外,一个问题,
因为DbGrid1向下(当然指最后一笔)移动时,DbGrid2第一笔自动去掉,
这会导致死循环,所以要加两个Boolean变量,当光标进入DbGrid1的时候
IsTable2 := True;另一个同理。我还是把代码贴出来给你参考吧。
---------------------只列主要部分,免得太长了-------------
procedure FormCreate(Sender: TObject);
procedure ADOTable3AfterScroll(DataSet: TDataSet);
procedure ADOTable2AfterScroll(DataSet: TDataSet);
procedure FormDestroy(Sender: TObject);
procedure DBGrid1Enter(Sender: TObject);
procedure DBGrid1Exit(Sender: TObject);
procedure DBGrid2Enter(Sender: TObject);
procedure DBGrid2Exit(Sender: TObject);
private
IsTable2, IsTable3: Boolean;
end;
-----------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
ADOTable1.First;
while not ADOTable1.Eof then
begin
if ADOTable1.RecNo <= (ADOTable1.RecordCount div 2) then
ADOTable2.AppendRecord([ADOTable1.FieldByName('').AsString,
ADOTable1.FieldByName('').AsString])//看你有多少字段
else
ADOTable3.AppendRecord([ADOTable1.FieldByName('').AsString,
ADOTable1.FieldByName('').AsString]);
ADOTable1.Next;
end;
end;
procedure TForm1.DBGrid1Enter(Sender: TObject);
begin
IsTable2 := True;
IsTable3 := False;
end;
procedure TForm1.DBGrid1Exit(Sender: TObject);
begin
IsTable2 := False;
end;
procedure TForm1.DBGrid2Enter(Sender: TObject);
begin
IsTable2 := False;
IsTable3 := True;
end;
procedure TForm1.DBGrid2Exit(Sender: TObject);
begin
IsTable2 := False;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ADOTable2.First;
while not ADOTable2.Eof do
ADOTable2.Delete;
if not ADOTable2.IsEmpty then
ADOTable2.Delete;
ADOTable3.First;
while not ADOTable3.Eof do
ADOTable3.Delete;
if not ADOTable3.IsEmpty then
ADOTable3.Delete;
end;
procedure TForm1.ADOTable3AfterScroll(DataSet: TDataSet);
begin
if IsTable3 then
if ADOTable3.Bof then
begin
ADOTable2.Last;
ADOTable3.InsertRecord([ADOTable2.FieldByName('').AsString,
ADOTable2.FieldByName('').AsString]);
ADOTable2.Delete;
end;
end;
procedure TForm1.ADOTable2AfterScroll(DataSet: TDataSet);
begin
if IsTable2 then
if ADOTable2.Eof then
begin
ADOTable3.First;
ADOTable2.InsertRecord([ADOTable3.FieldByName('').AsString,
ADOTable3.FieldByName('').AsString]);
ADOTable3.Delete;
end;
end;
//以上代码经过我的测试了,你先测一下吧,还有什么不满意的地方(如想建临时表
去做,那自己弄,hoho~~~~~~~)可以再探讨的嘛~~