我再给你一个方案:
=============
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGridTemp: TDBGrid;
DataSource1: TDataSource;
DBGrid: TDBGrid;
Button1: TButton;
DataSource2: TDataSource;
ADOConnection1: TADOConnection;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
str='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:/shiyan/TabNamesDAT.mdb;Persist Security Info=False';
procedure TForm1.Button1Click(Sender: TObject);
var
qry,qryTemp:TADOQuery;
i,j:integer;
s:array of string;
begin
qry:=TADOQuery.Create(nil);
qryTemp:=TADOQuery.Create(nil);
qry.ConnectionString:=str;
qryTemp.ConnectionString:=str;
with qry do
begin
Close;
SQL.Clear;
SQL.Add('select * from TabNames');
open;
end;
with qryTemp do
begin
Close;
SQL.Clear;
SQL.Add('select * from TabNamesTemp');
open;
end;
setLength(s,qry.RecordCount);
for i:=0 to qry.RecordCount-1 do
begin
if qry.FieldByName('FileName').AsString='123' then
s:='456';
//s:=qry.FieldByName('FileName').AsString;
qry.Next;
end;
qry.First;
for j:=0 to qry.RecordCount-1 do
begin
if s[j]='123' then
begin
qryTemp.Append;
qryTemp.FieldByName('FileName').asString:='456';
end;
qryTemp.Next;
end;
end;
//以下这个可以看到你添加的数据“456”在TDBGrid中
procedure TForm1.Button2Click(Sender: TObject);
var
qry,qryTemp:TADOQuery;
i:integer;
begin
qry:=TADOQuery.Create(nil);
qryTemp:=TADOQuery.Create(nil);
qry.ConnectionString:=str;
qryTemp.ConnectionString:=str;
with qry do
begin
Close;
SQL.Clear;
SQL.Add('select * from TabNames');
Open;
end;
with qryTemp do
begin
Close;
SQL.Clear;
SQL.Add('select * from TabNamesTemp');
Open;
end;
DataSource2.DataSet:=qryTemp;
DBGridTemp.DataSource:=DataSource2;
DataSource1.DataSet:=qry;
DBGrid.DataSource:=DataSource1;
end;
end.
=============
我试过了可以的。
虽然笨点儿,但是,很实用!你可以优化一下。