I am developing a database/intranet package for which I need to use a grid based on a TQuery, but I only need a few of the fields to show up in the grid. The wizard won’t let me do it? What am I missing/overlooking?
This is a stumbling block for a lot of people. And it’s not reallydue to missing something, it’s just due to unfamiliarity with the way thedata-aware components work. Think of the way data is displayed in Delphi asa layer cake:
The top layer is the component that displays the data; in your case, theTDBGrid. It’s connected to a DataSource component, which in turn is connectto TDataLinks or TFields. These are then connected to the underlying dataset.
You might think that the “visibility” of data is controlled by the DBGrid.Actually, data visibility is controlled at a much lower level, the TField level/Data-link level. I won’t go into TDataLink,because that’s a bit more on the esoteric side of things, but I will saythis: If you don’t use TField definitions for your dataset, Delphi willdefault to use the data links, which means that all your columns will bedisplayed. This is probably the behavior you’re seeing right now.
However, if you define TFields for your dataset component, only the onesyou add will be displayed in your grid or, if you’ve added all fieldsfrom the dataset to have TField definitions, only those fields that havetheir Visible property set to True will have their data displayed.
But how do you do define TFields for a dataset in the first place?
Well, I’m assuming that you have a TDBGrid, a TDataSource and a TQuery dropped onto your form. I’m further assuming that you’ve set up your query.To get at the TFields, all you have to do is double-click on the TQuerycomponent. This will bring up the Fields Editor. When you do this, it’ll beblank, but don’t worry. Press Ctrl-A to add fields, and an “Add Fields”dialog box will pop up, containing a list of available fields that can haveTField definitions attached to them. By default, all of the fields areselected. Press OK if you want all of them, or select only the fields youwant to display.
Once you’ve selected your fields, you’ll see them listed inthe fields editor. Click on one of the fields. Look in the ObjectInspector. You’ll see a bunch of properties associated with the field. Inparticular, look at the Visible property. By default, it will be set totrue. If you don’t want the data to be displayed, set the propertyto false.
That’s it in a nutshell. Refer to the online help for amore detailed explanation. Also, the Database Application Developer’s Guidethat came with your Delphi manual set will provide you with more robustinformation than I’ve given here.