I have a program in Delphi and in the database I have a table of statistics categories (it has id
and nombre
), with the idea of assigning each record producto
a id
category to group them.
I want to develop a form where when selecting a category of statistics from a grid or combo it shows me, in another grid, the products in that list (that is, those that have the one id
in that list in the table productos
) and, finally, another grid with those that are not associated, to be able to associate them.
I have a TClientDataSet
and a TDataSetProvider
next to a TDataSource
for the stat category table.
Then I did 1 TSQLQuery
with a parameter 'id'
:
SELECT * FROM productos WHERE Categoria_E=':id';
And this other one:
SELECT * FROM productos WHERE Categoria_E<>':id';
to associate them to the grids; also via a TClientDataSet
y provider .
It's okay like that?
If yes, in which event can I associate the value of the parameter id
to refresh the grid?
It is not necessary to use the quotes inside the SQL when you use a parameter, because for the connection layer, this is already a text string, to declare the type of the parameter as
string
, so your SQL should look more like this:Then assuming you have the following
TClientDataSet
declared:cdsCategoria
(select * from Categoria
)cdsProductoEnCategoria
(SELECT * FROM productos WHERE Categoria_E = :id
)cdsProductosNoCategoria
(SELECT * FROM productos WHERE coalesce(Categoria_E, '') = :id
)You can declare a method
RefrescarProductos
, like so:And finally call this method in the
OnAfterOpen
on eventsOnAfterScroll
of thecdsCategoria
, something like: