1. Computing
Drop down list (DBLookupComboBox) inside a DBGrid - part 2
Page 1: DBLookupComboBox 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
• Part 1: Three ways to set up a drop down list in a DBGrid.

• Page 2: DBLookupComboBox in a DBGrid!


• Download sample QuickiesContest.mdb database


• Download full source CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Adding components into a DBGrid - the theory

• free DB Course.TOC

• DBGrid related articles and tips

This is the third 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.

In the first part of this article (second in the series), we've discussed about lookup fields, and what are the options of displaying a lookup field in a DBGrid, it's time to see how to place a DBLookupComboBox into a cell of a DGBrid to enable a user to pick a value for a lookup field from a drop down list box.

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?
Yes, just as we can place a check box in a cell of a DBGrid (for boolean field editing), we can use the same technique to put a combo box into a cell holding a lookup field.
Recall that a DBLookupComboBox is used, in database applications, to present the user with a restricted list of choices from which to set a valid field value. When a user selects a list item, the corresponding field value is changed in the underlying dataset. In general, you use a DBLookupComboBox on a data entry form, separate from a DBGrid. This time we'll make a DBLookupComboBox float over a cell containing a lookup field.
Creating a sample application
Note that we've already created, in the previous article, a sample application containing a DBGrid and several DB related components. Here's how:
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 (again): nothing stops you from using any other Delphi's dataset components like TTable, TQuery, ...

Now, take a look at the AuthorEmail field. Note that this field can only accept values from the Email field of the Authors table (defined by referential integrity). What you need to "see" in the picture below is that, when editing this field wit no lookup defined, if you try to edit a value to some value not found in the Authors table ("master" table), you'll get "You cannot add or change a record because a related record is required in table Authors" error message.

Lookup field with no lookup defined

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

DBLookupComboBox in a DBGrid

Ok. First we need to add several more components on a form along with a DBLookupComboBox. Next we have to set them up and do some Delphi magic ;)
Interested? Go for the next page...

Next page > DBLookupComboBox in a DBGrid! > Page 1, 2

©2013 About.com. All rights reserved.