Board index » delphi » Replacing a foreign key with its value in a dbgrid

Replacing a foreign key with its value in a dbgrid

I have two tables Customer and CustomerTypes.

Customer:      CustNo  : INTEGER (primary key)
               CustType: INTEGER (foreign key references CustomerTypes No)
CustomerTypes: No      : INTEGER (primary key)
               Type    : VARCHAR(50)

My problem is now that if I show Customer in a dbgrid I want the field
CustType not to show the number but get via the number the Type from
CustomerTypes and show the Type-Stringfield.

Any ways to realize that?

Stefan
--------------------------------------------------------------------------
Stefan Westner                  E-Mail: Stefan.West...@stud.uni-bamberg.de
PGP-Public-Key auf Anfrage verfuegbar  PGP-Public-Key available on request
--------------------------------------------------------------------------
       The computer fanatic knows that computers are infallible...
            ...it's the Human Factor that messes thing up.

 

Re:Replacing a foreign key with its value in a dbgrid


Quote
Stefan Westner wrote:

> I have two tables Customer and CustomerTypes.

> Customer:      CustNo  : INTEGER (primary key)
>                CustType: INTEGER (foreign key references CustomerTypes No)
> CustomerTypes: No      : INTEGER (primary key)
>                Type    : VARCHAR(50)

> My problem is now that if I show Customer in a dbgrid I want the field
> CustType not to show the number but get via the number the Type from
> CustomerTypes and show the Type-Stringfield.

> Any ways to realize that?

Stefan,

You have to add a calculated field in your Customer's table. With Delphi
2, use the mouse right button on your customer's table component and
choose Field Editor; click again with the right button to choose New
Field from Field Editor. (With Delphi 1, double click on your customer's
table component, and choise Define to add one calculated field.) Once
this is done, just add a FindKey on the CustomerTypes's table in the
OnCalcFields events of your Customer's table.

Hope this helps.
--
Philippe Mundler

Re:Replacing a foreign key with its value in a dbgrid


Quote
Stefan.West...@stud.uni-bamberg.de (Stefan Westner) wrote:
>I have two tables Customer and CustomerTypes.
>Customer:      CustNo  : INTEGER (primary key)
>               CustType: INTEGER (foreign key references CustomerTypes No)
>CustomerTypes: No      : INTEGER (primary key)
>               Type    : VARCHAR(50)
>My problem is now that if I show Customer in a dbgrid I want the field
>CustType not to show the number but get via the number the Type from
>CustomerTypes and show the Type-Stringfield.

Use a TQuery object and do a join.

Select CustNo, Type
from Customer, CustomerTypes
where CustType =  No
-------------------------------
Thomas Paul
TOMG...@Pipeline.Com
Dean Witter, Discover & Co.
New York City, NY USA
----------------------------------

Other Threads