Jump to content
PjDS

Filter DBGrid by row (record) selected in another DBGrid

Recommended Posts

Dear Sirs,

in my Delphi 10.4 Form1 there are: ADOConnection1, ADOQuery1, DataSource1, DBGrid1 (TDBGrid component) and ADOConnection2, ADOQuery2, DataSource2, DBGrid2 (TDBGrid component). I use ADOConnection1, ADOQuery1, DataSource1 for polupate DBGrid1 and the other ones for populate DBGrid2. ADOConnection1 and 2 use the same access database (DB1.accdb). I would like to filter the DBGrid2 records using the selection of one record over the DBGrid1. The filter must use a Field of the selectd record (row) in DBGrid1. Questions:

    • Is it possible and Is the correct approach?
    • It would be preferable to create the connection to the database and manage it via code (runtime) without using components on the form (ADOConnection, ADOQuery, etc)?
    • Is it necessary to use two adoconnections if the database to which they are connected is the same?

Thanks!

Share this post


Link to post

1- It's possible but not the correcr approach

3- only one connection

2- use a parametrized query like SELECT * FROM <table> WHERE <fieldname>=:<fieldname of AdoQuery1> (note : really not easy without the relation fieldnames !)

    and set Adoquery1.mastersource to datasource2  

Share this post


Link to post
Guest

@PjDS

 

first, I think that you use a Master-Details technic. or be tbMaster have its details on tbDetails.

 

then  follow me:

 

1 Connection (just)

1 Query to Master table

1 Query to Details table (you can have many others if necessary for others details in others tables)

2 Datasources (for each querys.... more querys, more DataSources)

2 Grids (to show your data) ... as above

 

 

ADOconn = for your db string connection

ADOQryMaster Sql = select ID, ....* from tbXXXX

ADOQryDetail Sql = select ID, ID_Master,....* from tbZZZ where ID_Master = :ID   (this ID is from tbXXXX)

 

DSQryMaster = datasource to table Master

DSQryDetail = datasource to table Details

 

ADOQryDetail.MasterDatasource := DSQryMaster

ADOQryDetail.MasterFields = ID from master tb

 

DBGQryMaster.Datasource := DSQryMaster

DBGQryDetail.Datasource := DSQryDetail

 

it's ready!

just open query Master later Detail.

 

for create all in code just see like IDE define the properties in your unit.

 

using Ctrl+F12 you can see your Form like a "text"... press again and go back to Form format.

 

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×