这是 OP 在第一篇文章中提到的更正的工作代码。TClientDataset
您从DBGrid 中显示的内存表中获得。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBClient, Grids, DBGrids, StdCtrls, MidasLib;
type
TForm1 = class(TForm)
MemTable: TClientDataSet;
Button1: TButton;
Button2: TButton;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: word;
begin
MemTable.DisableControls;
for i := 1 to 20000 do
begin
MemTable.Append;
MemTable.FieldByName('ID').AsInteger := i;
MemTable.FieldByName('Status').AsString := 'Code'+IntToStr(i);
MemTable.FieldByName('Created').AsDateTime := Date();
MemTable.FieldByName('Volume').AsFloat := Random(10000);
MemTable.Post;
end;
MemTable.EnableControls;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MemTable.IndexFieldNames := 'Volume';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MemTable.FieldDefs.Add('ID', ftInteger, 0, False);
MemTable.FieldDefs.Add('Status', ftString, 10, False);
MemTable.FieldDefs.Add('Created', ftDate, 0, False);
MemTable.FieldDefs.Add('Volume', ftFloat, 0, False);
MemTable.CreateDataSet;
end;
end.