简体   繁体   English

在运行时重新分配数据源

[英]Reassigning a datasource at run-time

I did some searching and only found more unanswered questions. 我做了一些搜索,只找到了更多未解答的问题。 :) :)

Using D5pro. 使用D5pro。

I want to reassign the DataSource to a TDBGrid at run time. 我想在运行时将DataSource重新分配给TDBGrid。 I have seven identical structured DataSets and depending on a button click I want the appropriate DataSet displayed in the grid. 我有七个相同的结构化DataSet,根据按钮单击,我希望在网格中显示相应的DataSet。

I have tried everything and I cannot get it to show the next DataSet. 我已经尝试了一切,我无法让它显示下一个DataSet。 It sticks with the first one assigned at start up. 它坚持在启动时分配的第一个。 I am getting to overkill approaches and still nothing is working. 我正在采取过度杀伤方法,但仍然没有任何工作。 Here's where I am at the moment. 这就是我现在所处的位置。

procedure SetSource(var aSrc : TDataSource);
begin
  aSrc.DataSet.Close;
  dbgridShowData.DataSource:=aSrc;
  aSrc.DataSet.Open;
  aSrc.DataSet.First;
  aSrc.DataSet.Refresh;
end;

Where am I going wrong? 我哪里错了?

Thanks 谢谢

You can change the Dataset shown by a DBGrid quite easily at runtime quite easily. 您可以非常轻松地在运行时轻松更改DBGrid显示的数据集。 There two approaches: 有两种方法:

1: use a single DataSource assigned to DBGrid.DataSource and change the DataSource.DataSet to the desired DataSet. 1:使用分配给DBGrid.DataSource的单个DataSource,并将DataSource.DataSet更改为所需的DataSet。 Here is a simple example with all assignments made at runtime. 这是一个简单的示例,其中包含在运行时进行的所有分配。

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.DataSource := DataSource1;

  DataSet1.Active := true;
  DataSet2.Active := true;
  DataSet3.Active := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DataSource1.DataSet := DataSet1;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DataSource1.DataSet := DataSet2;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  DataSource1.DataSet := DataSet3;
end;

2: use a DataSource for each DataSet and change DBGrid.DataSource to the desired DataSource. 2:为每个DataSet使用DataSource,并将DBGrid.DataSource更改为所需的DataSource。 Here is a simple example with all assignments made at runtime. 这是一个简单的示例,其中包含在运行时进行的所有分配。

procedure TForm1.FormCreate(Sender: TObject);
begin
  DataSource1.DataSet := DataSet1;
  DataSource2.DataSet := DataSet2;
  DataSource3.DataSet := DataSet3;

  DataSet1.Active := true;
  DataSet2.Active := true;
  DataSet3.Active := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DBGrid1.DataSource := DataSource1;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DBGrid1.DataSource := DataSource2;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  DBGrid1.DataSource := DataSource3;
end;

If you define the columns of the DBGrid, the structure of the DataSets will need to be the the same, or you will have to change the column definitions when you change the Dataset displayed. 如果定义DBGrid的列,则DataSet的结构必须相同,或者在更改显示的数据集时必须更改列定义。

I prefer using a DataSource per DataSet because it is more flexible. 我更喜欢每个DataSet使用一个DataSource,因为它更灵活。

You probably need to change the DataSource.DataSet instead: 您可能需要更改DataSource.DataSet

procedure SetDataFromDataSet(const aDataSource: TDataSource;
  const aNewDataSet: TDataSet);
begin
  aDataSource.DataSet.Close;
  aDataSource.DataSet := aNewDataSet;
  if not aNewDataSet.Active then
    aNewDataSet.Open;
end;

Sample use: 样品用途:

SetDataFromDataSet(DataSource1, CustomerQuery); 

You may not want to close and open datasets globally like this, though. 但是,您可能不希望像这样全局关闭和打开数据集。 It's probably better to do that from the calling code. 从调用代码执行此操作可能更好。 Of course, that would depend on what you need for your app. 当然,这取决于您的应用程序所需的内容。

Tested with Delphi5 pro. 用Delphi5 pro测试。

procedure TForm1.setDataSourceDataSet(var newDataSource:TDataSource);
begin
if DBgrid1.DataSource = nil then begin
   DBgrid1.DataSource:=newDataSource;
end else begin
if DBgrid1.DataSource.Name = newDataSource.Name then exit;
DBGrid1.DataSource.Enabled:=False;
DBgrid1.DataSource:=newDataSource;
end;
If DBgrid1.DataSource.DataSet.active=False then DBgrid1.DataSource.DataSet.active:=True;
DBGrid1.DataSource.Enabled:=True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
setDataSourceDataSet(DataSource1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
setDataSourceDataSet(DataSource2);
end;

The secret lies in: 秘诀在于:

DBGrid1.DataSource.Enabled:=False; DBGrid1.DataSource.Enabled:= FALSE; ...making changes... DBGrid1.DataSource.Enabled:=True; ...进行更改... DBGrid1.DataSource.Enabled:= True;

Tested with D5Pro 用D5Pro测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM