Question

I am taking my first stumbling steps into DB aware controls (any good tutorials?).

I have a MySql table with 6 columns and have managed to load it into a TDbGrid.

One of the columns however is an index into another table. It is a bar code and, rather than display that, I would like to display the product name associated with it.

How do I do that?

(and can I hide the "gutter" (?) down the left whcih shows the current row?)

Thanks

Was it helpful?

Solution

You should always perform a join from the SQL side, it's much easier then doing it programaticaly

Such as:

SELECT mytable.id, mytable.column1, another_table.barcode
FROM mytable
JOIN another_table ON another_table.id = mytable.barcode_id

To remove gutter you need to uncheck the DBGrid property dgIndicator in Options.

As for "DB-Aware controls" you should try delphi help.

OTHER TIPS

Instead of a table, make use of a query. Then, use a join to select the product name with it, like this:

SELECT
  t.*,
  p.name
FROM
  YourTable t
  INNER JOIN Product p on p.barcode = t.barcode

I use t.*, because I don't know the exact columns. In practise, I would not use select *, but specify specific columns instead. If you are going to use * anyway, you can hide speicfic columns by setting the Visible property of the TField object in the dataset/query to False.

I don't know which components you are using to connect to the table, but most of them do have a query-counterpart that allows you to insert SQL instead of a table name.

The gutter can be hidden by going to the property Options in the object inspector, expand it, and set dgIndicator to False.

Just for the record: with ISAM databases like Paradox and DBF typical solution would be so-called master-detail tables relations and it still might work for SQL. Though it would be very inefficient and slow. You'd definitely read som books about SQL.

Use a TQuery component instead of a TTable and set SQL property using the suggested select statements above. If you just add the columns you want to display in your sql statement, you get the result as expected. As for "gutter" you would have to hack the grid in some way at runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top