在运行时重新分配数据源 [英] Reassigning a datasource at run-time

查看:89
本文介绍了在运行时重新分配数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一些搜索,只发现了更多未解决的问题。 :)

I did some searching and only found more unanswered questions. :)

使用D5pro。

我想在运行时将数据源重新分配给TDBGrid。我有七个相同的结构化数据集,然后单击按钮,我希望在网格中显示适当的数据集。

I want to reassign the DataSource to a TDBGrid at run time. I have seven identical structured DataSets and depending on a button click I want the appropriate DataSet displayed in the grid.

我已经尝试了所有方法,但无法显示下一个数据集。它坚持在启动时分配的第一个。我正在过度使用方法,但仍然无济于事。

I have tried everything and I cannot get it to show the next 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;

我要去哪里了?

谢谢

推荐答案

您可以在运行时轻松地更改DBGrid显示的数据集。有两种方法:

You can change the Dataset shown by a DBGrid quite easily at runtime quite easily. There two approaches:

1:使用分配给DBGrid.DataSource的单个DataSource并将DataSource.DataSet更改为所需的DataSet。这是一个简单的示例,其中包含所有在运行时进行的分配。

1: use a single DataSource assigned to DBGrid.DataSource and change the DataSource.DataSet to the desired 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:为每个数据集使用一个数据源,并将DBGrid.DataSource更改为所需的数据源。这是一个简单的示例,其中包含所有在运行时进行的分配。

2: use a DataSource for each DataSet and change DBGrid.DataSource to the desired 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;

如果定义DBGrid的列,则数据集的结构必须相同,否则在更改显示的数据集时将不得不更改列的定义。

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.

我更喜欢为每个数据集使用一个数据源,因为它更灵活。

I prefer using a DataSource per DataSet because it is more flexible.

这篇关于在运行时重新分配数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆