Reassigning a datasource at run-time

对着背影说爱祢 提交于 2019-12-12 12:24:49

问题


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

Using D5pro.

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;

Where am I going wrong?

Thanks


回答1:


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

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: 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;

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.




回答2:


You probably need to change the DataSource.DataSet instead:

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.




回答3:


Tested with 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;



回答4:


The secret lies in:

DBGrid1.DataSource.Enabled:=False; ...making changes... DBGrid1.DataSource.Enabled:=True;

Tested with D5Pro



来源:https://stackoverflow.com/questions/15397135/reassigning-a-datasource-at-run-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!