Runtime data source reassignment

I did a few searches and found more unanswered questions. :)

Using D5pro.

I want to reassign a DataSource TDBGrid at runtime. I have seven identical structured data sets and depending on the button click I want the corresponding DataSet to appear in the grid.

I tried everything and I can not get it to show the next DataSet. He sticks to the first one assigned at startup. I get to outsmart approaches and still nothing works. Here where I am now.

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

+4
source share
4 answers

You can easily easily change the dataset shown by DBGrid at runtime. There are two approaches:

1: use one data source assigned to DBGrid.DataSource, and change the DataSource.DataSet to the desired DataSet. Here is a simple example with all the 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 data source. Here is a simple example with all the 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 DBGrid columns, the DataSets structure must be the same, or you will have to change the column definitions when changing the displayed dataset.

I prefer to use a DataSource for each dataset because it is more flexible.

+5
source

You probably need to change the 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; 

Using an example:

 SetDataFromDataSet(DataSource1, CustomerQuery); 

You may not want to close and open datasets around the world. It is probably best to do this from the calling code. Of course, this will depend on what you need for your application.

+3
source

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; 
+1
source

The secret lies in:

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

Tested with D5Pro

-1
source

Source: https://habr.com/ru/post/1469080/


All Articles