You are here:
About>Computing & Technology>Delphi Programming
About.com

FREE Newsletter


See Online Courses
 
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

Your Guide, Zarko GajicFrom Zarko Gajic,
Your Guide to Delphi Programming.
FREE Newsletter. Sign Up Now!

    Topic Index | Email to a Friend

Our Story | Be a Guide | Advertising Info | Work at About | Site Map | Icons | Help
User Agreement | Ethics Policy | Patent Info. | Privacy Policy | Kids' Privacy Policy

©2005 About, Inc., A part of the New York Times Company. All rights reserved.
Around About

New Video iPod - Review POLL: Must-Have Gadgets VIDEO: Portable Gaming VIDEO: Choosing a TV Hugh Hefner's Life

What's Hot

Delphi Drawing Snowflakes over your Desktop ScreenLeftStr functionDelphi Keyboard ShortcutsA more powerful Delphi Form - page 1/2Uploading files to a web server with ASP.Net

Headlines

Understanding Delphi Variable Scope ARTICLES :: The term scope refers to the availability of...Inside the EXE eCourse :: Storing more that just code in the application...Drawing Snowflakes All Over your Desktop Screen FUN :: It's time for a little winter joy! Amaze...Delphi IDE (Code Editor) Keyboard Shortcuts TIPS :: Every once in a while I'll come across...