1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Adding components to a DBGrid
How to place just about any Delphi control (visual component) into a cell of a DGBrid. Find out how to put a CheckBox, a ComboBox (drop down list box), a DateTimePicker (calendar) and even an Image inside the DBGrid.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Download sample MS Access database
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• free DB Course.TOC
• Coloring DBGrid
• Multiple row selection in DBGrid
• Sorting a DBGrid by Column click
• Using Delphi DB components
• more Database articles

The power of the DBGrid
I've said this many times, but I'll say it again: Delphi's DBGrid ... what a powerful component! Contrary to most other Delphi data-aware controls, the DBGrid component has many nice features and is more powerful than you would have thought.

The "standard" DBGrid does its job of displaying and manipulating records from a dataset in a tabular grid. However, there are many ways (and reasons) why you should consider customizing the output of a DBGrid.
Here's an example:
Suppose you have a boolean field in your dataset. By default, the DBGrid displays boolean fields as "True" or "False" depending on the value of the data field. If you think the same way I do, it is much more visually attractive to be able to you use a "true" check box control to enable editing of such fields.

This article serves as an entry point to a series of articles describing how to place just about any component into a cell of a DBGrid.

Placing controls in DBGrid
As some of you might not know, in-place editing of a DBGrid cell's contents is accomplished via a "small" edit control that is displayed over the cell. Inside DBGrid, there is a TInplaceEdit that moves around the grid - the Edit component that you enter your data into. The rest of the unfocused cells are really just "pictures".

But a CheckBox, a ComboBox, ...?
So what? If there is a floating Edit over the DBGrid, we'll float any control we like. There are no new ideas here, in fact, the basic technique simply mimics what the DBGrid does internally. What you will learn here, is how to float any type of visual control around the DBGrid.

A sample database
Following the concepts set out in the Beginners Guide to Delphi Database Programming, examples below use ADO components (AdoQuery/AdoTable connected to ADOConnection, DBGrid connected to AdoQuery over DataSource) to display the records from a database table in a DBGrid component. All the component names were left as Delphi named them when dropped on the form (DBGrid1, ADOQuery1, AdoTable1, ...)
If you do not know how to display records from a database table (or query) in a DBGrid component, please explore the "Connecting to a database" chapter.

Here's a sample MS Access database we'll use to explore the topic of adding components to a DBGrid. The QuickiesContest.MDB databse has three tables: Subjects, Authors and Articles. The database was originally created to hold the entries to our Delphi Programming Quickies Contest.
The picture below displays the relationships between tables. For the moment note that the Winner field in the Articles table is a YesNo field (boolean).

Delphi Quickies Contest database
Download database
Components in a DBGrid - the Theory
When adding controls to a DBGrid, there are a couple of steps and questions to be revealed. Here's what has to be done to place a DBCheckBox inside a DBGrid cell, and enable editing of a boolean field using the CheckBox component:
  1. A DBCheckBox needs to be placed (invisible) on a Form and linked to a boolean field in the DataSource component that supplies the DBGrid with a dataset.
  2. If the cell holding a boolean field is focused the CheckBox component should be made visible and placed in the appropriate cell. When no longer needed the CheckBox needs to be made invisible again.
  3. If the cell holding a boolean field is NOT focused a sample graphics should be drawn on the cell indicating whether a field's value is True or False.
  4. When in editing mode, all keystrokes are going to the DBGrid's cell; we have to make sure they are sent to the CheckBox. Note that we are primarily interested in the [Tab] and the [Space] key. "Tab" should move the input focus to the next cell, and [Space] should toggle the state of the CheckBox.
Note: the above "steps" are more or less the same no matter what component is placed in (i.e. floating over) the cell.
Components in a DBGrid - Theory into Practice
Finally, here's how all the above theory looks in practice (with more examples coming in the near future):

Placing a CheckBox in a DBGrid
Here's how to place a check box into a DBGrid. Create visually more attractive user interfaces for editing boolean fields inside a DBGrid.

DBCheckBox in DBGrid

Drop down pick list inside a DBGrid - part 1
Here's how to place a drop down pick list into a DBGrid. Create visually more attractive user interfaces for editing lookup fields inside a DBGrid - using the PickList property of a DBGrid column.

Lookup field with PickList

Drop down list (DBLookupComboBox) inside a DBGrid - part 2
Here's how to place a DBLookupComboBox into a DBGrid. Create visually more attractive user interfaces for editing lookup fields inside a DBGrid - place a DBLookupComboBox into a cell of a DBGrid.

DBLookupComboBox in a DBGrid

DateTimePicker inside a DBGrid
Here's how to place a TDateTimePicker into a DBGrid. Create visually more attractive user interfaces for editing date/time fields inside a DBGrid - place a drop down calendar into a cell of a DBGrid.

DateTimePicker in a DBGrid

Need more DBGrid related articles?
Be sure to check the rest of the articles dealing with the DBGrid (and other db-aware) components; of course don't miss the "DBGrid to the Max" article collection!

A Beginner's Guide to Delphi Database Programming >>
>> the TOC

©2016 About.com. All rights reserved.