1. Home
  2. Computing & Technology
  3. Delphi Programming
Lookup!
Page 4: DBGrid.Columns.PickList as a lookup list.
 More of this Feature
• Page 1: On lookup fields
• Page 2: New...lookup
• Page 3: DBLookupCombo

• Project's CODE

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
 Related Resources
• free DB Course.TOC
• Coloring the DBGrid
• Using Data Controls
• more Database articles

   Lookup inside a PickList of a DBGrid Column
The last approach to having a lookup values displayed inside a DBGrid is to use the PickList property of a DBGrid Column object. You'll usually add Columns to a DBGird when you want to define how a column appears and how the data in the column is displayed. A customized grid enables you to configure multiple columns to present different views of the same dataset (different column orders, different field choices, and different column colors and fonts, for example).

I will not discuss this topic briefly here. Let's just see what are the steps to add columns to a DBGrid.
1. Right-click the DBGgrid component. This pops up the Columns Editor.
2. Right-click the Columns Editor to invoke the context menu and choose Add All Fields. This creates one column for each field in a dataset and connects them - the FieldName property determines the connection.

While you have the Columns Editor displayed, see that each column has a PickList property. We'll use this String List to fill a list of lookup values. Take a look at the AEMail column, its FieldName points to a lookup field - therefore displaying a combo box inside a DBGrid as we saw at the beginning of this article. However its PickList is empty. What I want to show you here is how to fill that String List with values from another dataset at run time - without creating a new lookup field. In general, a pick list column looks and works like a lookup list, except that the drop-down list is populated with the list of values in the column's PickList property instead of from a lookup table. The reason to show how to use PickList to mimic the lookup list is just to show that the same task can be done in several ways when you have a great toll like Delphi.

What we want to do is to fill a PickList with the values from another dataset at run time - this time using Pascal code. We'll use the Form's OnCreate event.

procedure TForm1.FormCreate(Sender: TObject);
var AuthorsList:TStringList;
    i:integer;
begin
  AuthorsList:=TStringList.Create;
  try
    AuthorsTable.First;
    AuthorsTable.DisableControls;
    while not AuthorsTable.EOF do begin
      AuthorsList.Add(
	     AuthorsTable.FieldByName(
		    'authorname').AsString);
      AuthorsTable.Next;
    end; //while
  finally
    //place the list it the correct column
    for i:=0 to DBGrid1.Columns.Count-1 do begin
      if DBGrid1.Columns[i].FieldName = 'Author'
      then begin
        DBGrid1.Columns[i].PickList:=AuthorsList;
        Break;
      end;//if
    end; //for
    AuthorsTable.EnableControls;
    AuthorsList.Free;
  end; //try
end;

The code simply fills the AuthorsList with the values of AuthorName, by iterating through the Authors table. It then assigns the contents of the AuthorsList to the PickList whose FiledName point to the Author field. Since we can change the order of columns at design and run time you have to make sure that the correct PickList get's the values.

That's all for this chapter. Make sure you download code for this project.

   To the next chapter
If you need any kind of help so far, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

First page > Why and when to use lookup fields; Creating a data entry form > Page 1, 2, 3, 4

DB Course Next Chapter >>
>> Compacting an Access database with ADO and Delphi - DB/16

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.