本文来自:万一
本例效果图:
代码文件:
本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; Button2: TButton; ClientDataSet1: TClientDataSet; DataSource1: TDataSource; DBGrid1: TDBGrid; procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {本来可以不写代码, 设置四个属性即可} procedure TForm1.FormCreate(Sender: TObject); var DataFile: string; begin {获取 CodeGear 共享安装的数据文件: country.xml 的路径} DataFile := GetEnvironmentVariable('COMMONPROGRAMFILES'); {C:\Program Files\Common Files} DataFile := DataFile + '\CodeGear Shared\Data\country.xml'; {country.cds 也可} DBGrid1.DataSource := DataSource1; {DBGrid 需要连接数据源} DataSource1.DataSet := ClientDataSet1; {数据源 DataSource 需要连接数据集} ClientDataSet1.FileName := DataFile; {让数据集 ClientDataSet 关联一个数据文件} {打开数据集} ClientDataSet1.Active := True; //或 ClientDataSet1.Open; end; end.