Filtering a TDataSet as detail of an object
Having explored how to link to a Master Detail relationship and created a funky anonymous method to return the data, I thought there is no reason why I should have to be returning linked data. The data could be from anywhere.
One example people have asked about when I’ve been at developer events is how to link an object to be the master for filtering a TDataSet… well, this actually provides a simple example for doing exactly this (as you can see in this video)
In the previous examples I have being using a TVLBSyncMasterList instance to control create the BindsSourceAdapter, with associated AfterScroll and BeforeScroll events to notify bindings managing the detail objects. In this example I in essence doing the same thing, but with a twist. I am adding in code to filter the TDataSet using the filter option to match the current selected object in a list.
The code for the project in the video is available from Code Centreal http://cc.embarcadero.com/item/30448 and has been updated to fix a memory leak (so if you downloaded before 27th November 2015, please download a fresh).
In short, using TVLBSyncDetailObjectLink as an item in the DetailsArray I am able to trap when the data is changing on the master and get the current TFoo object. Using an anonymous method as before we then code the action. Rather than returning the DataSet for binding I am filtering the DataSet that is linked using TDataSource and TDBGrid. (well it is a VCL application).
DetailsArray := TVLBSyncDetailObjectLink<Tfoo, TDataSet>.Create(FooList, function(aCurrentMaster : TFoo) : TDataSet begin if not Assigned(FDMemTable1) then Exit(nil); if aCurrentMaster = nil then begin Result := nil; DBGrid1.Visible := False; end else begin Result := FDMemTable1; FDMemTable1.Filter := 'NAME='+aCurrentMaster.Name.QuotedString; FDMemTable1.Filtered := True; DBGrid1.Visible := True; end; end, );
You could equally use this approach to fetch data on demand, refresh a query with different parameters etc.