Jump to content
PiedSoftware

Ho to list all data-aware controls attached to a datasource

Recommended Posts

Hi
How can I get a list of all the data-aware controls whose datasource is one that is specified?
 

I have googled and even consulted Claude, without success, but I thought there was a way.

Any ideas?

Share this post


Link to post

You can iterate the protected DataLinks property (there are several ways to access a protected property described elsewhere). For each TDataLink in this list act according to the actual class type: A TFieldDataLink gives you access to the corresponding Control, while a TGridDataLink provides the connected Grid. There are other classes inherited from TDataLink with other purposes, but that should you get started.

Share this post


Link to post

Thanks for the thought, Uwe. I should have said I'm using Delphi 10.4. I tried this function:
 

uses Vcl.DBCtrls, System.Generics.Collections, System.Classes;

type TLocalDataSource = class(TDataSource);

function GetDataAwareList(ds: TDataSource): TArray<TControl>;
var lds: TLocalDataSource absolute ds;
    links: TList<TDataLink>;
    link: TDataLink;
    fldLink: TFieldDataLink;
    cmp: TComponent;
begin
  result := [];
  links := lds.DataLinks;
  for link in links do begin
    if link is TFieldDataLink then begin
      fldLink := link as TFieldDataLink;
      cmp := fldLink.Control;
      if cmp is TControl then begin
        result := result + [TControl(cmp)];
      end;
    end;
  end;
end{ GetDataAwareList};

It keeps seizing in the middle there, no particular line, sometimes reading fldlink.Control, sometimes when adding it to the array. I've been hacking at that for a while, stretching it out with more local variables to make it finer grained, but it has never worked for me. 

I'll just have to go ahead with code that is aware of what controls it needs to manage.

 

 

EDIT: It seems that it does actually work. It was the debugger that was freezing. When I don't single step through it, it works fine.

Edited by PiedSoftware

Share this post


Link to post

Here is the code when reduced in size because it works:

 

type TLocalDataSource = class(TDataSource); // Exposes protected property DataLinks

function GetDataAwareList(ds: TDataSource): TArray<TControl>;
var lds: TLocalDataSource absolute ds;
    link: TDataLink;
    fldLink: TFieldDataLink absolute link;
begin
  result := [];
  for link in lds.DataLinks do begin       // See, I told you
    if (link is TFieldDataLink) and (fldLink.Control is TControl) then begin
      result := result + [TControl(fldLink.Control)];
    end;
  end;
end{ GetDataAwareList};

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×