0
votes

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

4
you can spare yourself all the pain and hassle and invest in a professional grid component like devexpress, tms, ... - whosrdaddy
@whosrdaddy Using a 'professional' grid can be handy if you develop professional applications, but we don't know if that is the case here, and above that, none of those grids solves this problem, since it still needs to fetch data from another table. - GolezTrol
@GolezTrol, I wasn't talking about fetching the data, The standard DB controls can become quickly cumbersome to work with.... - whosrdaddy
@GolezTrol: Devexpress Grids solve that problem, because you have lookup-columns, you just have to assign the lookup-table. - Andreas
Nice, I didn't know that. :-) Which is part of the trouble with DevExpress grids. I've used them for years (the cxGrid and the dxGrid before that), and I've never felt I could grasp the full potential of the components. They are about a complete application in a grid, but with a very steep learning curve. And they add a lot of weight to your application. Although I sincerely agree that TDBGrid is too primitive, I also believe many 3rd party grids are 'better' than DevExpress, just because DX is overly complex. (Not because of these lookup columns, which actually make sense.) - GolezTrol

4 Answers

4
votes

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.

2
votes

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.

0
votes

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.

-1
votes

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.