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

How to set the DataSource property to several db-aware controls in one call

By Zarko Gajic, About.com

If you need to change the DataSource property of many data-aware controls that do not have a common ancestor you can use Delphi's RTTI (run-time type info) and call only one procedure. Here's how:

~~~~~~~~~~~~~~~~~~~~~~~~~
uses TypInfo;
...
procedure ApplyDataSource(dbCtrls: array of TControl; DS: TDataSource) ;
var
   cnt: Integer;
   PropInfo: PPropInfo;
begin
   for cnt := Low(dbCtrls) to High(dbCtrls) do
   begin
     PropInfo := GetPropInfo(dbCtrls[cnt].ClassInfo, 'DataSource') ;
     if Assigned(PropInfo) then
       SetOrdProp(dbCtrls[cnt], PropInfo, LongInt(DS)) ;
   end;
end;
...

Usage:

ApplyDataSource([DBNavigator1, DBText1, DBButton1], DataSource1) ;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» What does #13#10 stand for, in Delphi code?
« Fixing the RadioButtonList ASP.NET Web Server control - adding Attributes to Items

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. How to set the DataSource property to several db-aware controls in one call

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

All rights reserved.