|Controlling the Field Display in a TDBGrid|
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 really due to missing something, it's just due to unfamiliarity with the way the data-aware components work. Think of the way data is displayed in Delphi as a layer cake:
The top layer is the component that displays the data; in your case, the TDBGrid. It's connected to a DataSource component, which in turn is connect to 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 say this: If you don't use TField definitions for your dataset, Delphi will default to use the data links, which means that all your columns will be displayed. This is probably the behavior you're seeing right now.
However, if you define TFields for your dataset component, only the ones you add will be displayed in your grid or, if you've added all fields from the dataset to have TField definitions, only those fields that have their 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 TQuery component. This will bring up the Fields Editor. When you do this, it'll be blank, 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 have TField definitions attached to them. By default, all of the fields are selected. Press OK if you want all of them, or select only the fields you want to display.
Once you've selected your fields, you'll see them listed in the fields editor. Click on one of the fields. Look in the Object Inspector. You'll see a bunch of properties associated with the field. In particular, look at the Visible property. By default, it will be set to true. If you don't want the data to be displayed, set the property to false.
That's it in a nutshell. Refer to the online help for a more detailed explanation. Also, the Database Application Developer's Guide that came with your Delphi manual set will provide you with more robust information than I've given here.
Copyright © 1995, 1996, 1997 Brendan V. Delumpa All Rights Reserved
|Copyright © The Delphi Corner 2001 All Rights Reserved|