| Drop down list inside a DBGrid - part 1 | ||||||||||||||||||||||||||
| Page 2: defining a PickList for a (lookup) field. Making the drop-down list appear faster. | ||||||||||||||||||||||||||
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.
Now, that you know what are lookup fields, and what are the options of displaying a lookup field in Delphi's DBGrid, it's time to see how to use the PickList property of a DGBrid column to enable a user to pick a value for a lookup field from a drop down list box.
Now, each Column in a grid is "linked" to a field from a dataset displayed in the grid. What's more, each column has a PickList property. The PickList property lists values that the user can select for the column's linked field value.
Here's how to set up the PickList property. First, we add a call to the SetupGridPickList procedure in the Form's OnCreate event handler.
The easiest way to create the SetupGridPickList procedure is to go to the private part of the form declaration, add the declaration there and hit the CTRL + SHIF + C key combination - Delphi's code completion will do the rest:
Note: the SetupGridPickList procedure takes two parameters. The first parameter, FieldName, is the name of the field we want to act like a lookup field; the second parameter, sql, is the SQL expression we use to populate the PickList with possible values - in general the SQL expression should return a datataset with only one field.
Here's how the SetupGridPickList looks like:
That's it. Now, when you click the Subject column (to enter into edit mode), you'll get the next picture:
![]() Note 1: by default, the drop-down list displays 7 values. You can change the length of this list by setting the DropDownRows property. Note 2: nothing stops you from filling up the PickList from a list of values not coming from a database table. If, for example, you have a field that only accepts week day names ('Monday', ..., 'Sunday') you can build a "hard-coded" PickList. "Uh, I need to click the PickList 4 times..."Note that when you want to edit the field displaying a drop down list, you'll need to click the cell 4 times in order to actually pick a value from a list. The next code snippet, added to the DBGrid's OnCellClick event handler, mimics a hit to the F2 key followed by Alt + DownArrow.
That's all folksThis concludes Part 1 of this article; in the next two parts you'll learn how to place a DBLookupComboBox inside a cell of a DBGrid.If you need any help, please post all your problems (and/or suggestions) to the About Delphi Programming Forum. First page > Three ways to set up a drop down list in a DBGrid. > Page 1, 2 |
||||||||||||||||||||||||||


