救救我啊,小弟最后25分了! ( 积分: 25 )

  • 主题发起人 主题发起人 maysoft
  • 开始时间 开始时间
M

maysoft

Unregistered / Unconfirmed
GUEST, unregistred user!
我用Adoquery查询出数据,那么怎么修改字段A1下的值为‘123’替换为'456'让DBGrid显示,但是并不把修改后的结果提交到数据库!
 
做一个临时表:先把123数据检索出来用456替换,然后存到临时表。用临时表梆到DBGrid,这不就成了吗!
 
在ADOQuery上双击 添加所有字段,在A1的ONGetData事件里写
if Sender.AsString = '123' then
Text = '456'
 
procedure TForm1.ADOQuery1KGRQGetText(Sender: TField;
var Text: string; DisplayText: Boolean);
begin
if text = '1899-12-30' then
Text := '';
end;
我发现DBgrid显示该字段下的值都空了!
 
我再给你一个方案:
=============
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.


=============
我试过了可以的。
虽然笨点儿,但是,很实用!你可以优化一下。
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
923
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部