1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Drop down list inside a DBGrid - part 1
Page 1: three ways to set up a drop down list in a 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
• Page 2: Defining a PickList for a field

• Download sample QuickiesContest.mdb database
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Adding components into a DBGrid - the theory

• free DB Course.TOC
• DBGrid related articles and tips

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.

This is the second article, in the series of articles named "Adding components to a DBGrid". The idea is to show how to place just about any Delphi control (visual component) into a cell of a DGBrid. If you are unfamiliar with the idea, please first read the "Adding components to a DBGrid" article.

Drop down pick list in a DBGrid?
As discussed in the above article, there are many ways (and reasons) why you should consider customizing the output of a DBGrid: suppose you have a lookup field in your dataset. One way to explain a database lookup field is to say that lookup fields are fields that can *only* accept values from a fixed list of values ("static" or from a dataset). Here's an example: if you have a Products table with one field holding the name of the supplier, when editing that field it would be great to enable a user to pick a supplier name from a list of suppliers (contained in the Suppliers table)
Creating a sample application
To begin, start Delphi and, on that default empty new form, place a TDBGrid, a TADOTable, and a TADOConnection, TDataSource. Leave all the component names as Delphi named them when dropped on the form (DBGrid1, ADOQuery1, AdoTable1, ...). Use the Object Inspector to set a ConnectionString property of the ADOConnection1 (TADOConnection) component to point to the sample QuickiesContest.mdb MS Access database. Connect DBGrid1 to DataSource1, DataSource1 to ADOTable1, and finally ADOTable1 to ADOConnection1. ADOTable1's TableName property should point to the Articles table (thus making the DBGrid display the records of the Articles table).

Note: Nothing stops you from using any other Delphi's dataset components like TTable, TQuery, ...

If you have set all the properties correctly, when you run the application (given that the Active property of the ADOTable1 component is True) you should see the following output:

Lookup field with no PickList

Note that the Subject field can only accept values from the Subjects table (defined by referential integrity). What you need to "see" in the above picture is that, when editing this field wit no pick list, if you try to edit a value to some value not found in the Subjects table ("master" table), you'll get "You cannot add or change a record because a related record is required in table Subjects" error message

What we are up against in this article is to make the above picture look like the one below:

Lookup field with PickList

There are three ways you can set a lookup field in Delphi.

  1. If you have a data form where all editings are done in a DBGrid, the best is to create a new (lookup) field for the dataset.
  2. If a form hosts a set of data controls (DBEdit, DBComboBox, etc.), it makes sense to just use DBLookupComboBox without creating a new field.
  3. The last one is to use columns of a DBGrid with its PickList property, again without creating a new field. This approach is not a true lookup approach but you'll see that it can act as one. The PickList list values that the user can select for the field value - just like a standard DBComboBox - but inside a DBGrid.
We'll discuss each of them, starting with the last.

On the next page you'll find out how to make a DBGrid column's cell display a drop-down list of values.

Note: if you are interested in placing a DBLookupComboBox into a DBGrid's cell, take a look at the "Drop down list (DBLookupComboBox) inside a DBGrid - part 2" article.

Next page > Defining a PickList for a field > Page 1, 2

©2014 About.com. All rights reserved.