TdxMemData控件可以把DataSet数据库保存在内存中,并显示在TdxDBGrid上,而通过dxmdt1.FieldByName(‘字段‘).value得到当前光标所在值。通过while遍历 整个数据集中的值。如此可以在内存中先操作数据然后再用sql语句提交到数据库, 通常对一条记录都有增删改的操作,内部的操作可以这样实现:增删改按钮下只是操作内存中的数据,当真正保存按钮时才提交到数据库。方便数据的处理。
TdxMemData应用示例代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, dxExEdtr, dxDBTLCl, dxGrClms, dxDBCtrl,
dxDBGrid, dxTL, dxCntner, dxmdaset, Grids, DBGrids;
type
TForm1 = class(TForm)
qry1: TADOQuery;
con1: TADOConnection;
mmo1: TMemo;
btn1: TButton;
dxmdt1: TdxMemData;
dxdbgrd1: TdxDBGrid;
ds1: TDataSource;
intgrflddxmdt1orderid: TIntegerField;
intgrflddxmdt1custid: TIntegerField;
intgrflddxmdt1empid: TIntegerField;
dtflddxmdt1orderdate: TDateField;
strngflddxmdt1shipaddress: TStringField;
dxdbgrdclmndxdbgrd1RecId: TdxDBGridColumn;
dxdbgrdmskclmndxdbgrd1orderid: TdxDBGridMaskColumn;
dxdbgrdmskclmndxdbgrd1custid: TdxDBGridMaskColumn;
dxdbgrdmskclmndxdbgrd1empid: TdxDBGridMaskColumn;
dxdbgrdtclmndxdbgrd1orderdate: TdxDBGridDateColumn;
dxdbgrdmskclmndxdbgrd1shipaddress: TdxDBGridMaskColumn;
btn2: TButton;
dbgrd1: TDBGrid;
btn3: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
i:Integer;
begin
i:=1;
qry1.Connection:=con1;
qry1.SQL.Clear;
qry1.SQL.Add(‘select top 12 orderid,custid,empid,orderdate,shipaddress from TSQLFundamentals2008.sales.Orders‘);
qry1.Open;
qry1.First;
while not qry1.Eof do
begin
dxmdt1.Append; //从数据为读取值添加到内存中
dxmdt1.FieldByName(‘RecId‘).AsInteger:=(i+1);
dxmdt1.FieldByName(‘orderid‘).AsInteger:=StrToInt(qry1.FieldByName(‘orderid‘).asstring);
dxmdt1.FieldByName(‘custid‘).AsInteger:=StrToInt(qry1.FieldByName(‘custid‘).asstring);
dxmdt1.FieldByName(‘empid‘).AsInteger:=StrToInt(qry1.FieldByName(‘empid‘).asstring);
dxmdt1.FieldByName(‘orderdate‘).AsDateTime:= StrToDate(qry1.FieldByName(‘orderdate‘).asstring);
dxmdt1.FieldByName(‘shipaddress‘).AsString:=qry1.FieldByName(‘shipaddress‘).asstring;
mmo1.Lines.Add(qry1.FieldByName(‘orderdate‘).AsString) ;
qry1.Next;
inc(i);
end;
qry1.Close;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
dxmdt1.First;
while not dxmdt1.Eof do
begin
if (dxmdt1.FieldByName(‘orderid‘).AsInteger=10249) or (dxmdt1.FieldByName(‘orderid‘).AsInteger=10250) then
begin
dxmdt1.Edit; //需要先设置为edit模式
dxmdt1.FieldByName(‘shipaddress‘).AsString:=‘zheng zhou‘;
end;
dxmdt1.Next;
end;
dxmdt1.Edit;
dxmdt1.Post;
dxmdt1.First;
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
end;
end.