片断,谨供参考:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,db, dbtables,dbgrids;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
tb:TTable;
ds:TDataSource;
DG:TDbGrid;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
tb:=TTable.Create(form1);
tb.DatabaseName:='DBDEMOS';
tb.TableName:='clients.dbf';
ds:=TDataSource.Create(form1);
ds.DataSet:=tb;
dg:=TDBGrid.Create(form1);
dg.Align:=alTop;
dg.Height:=500;
dg.Parent:=form1;
dg.DataSource:=ds;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
tb.Active:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
tb.Active:=false;
end;
end.