How to create a DATASOURCE property

I want to know how to create a datasource property in an own written component and to access the datasource, that means how to get the data from ttable or tquery ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Declare a PROPERTY  of type TDataSource in your component.  Make if reference a private field like FDataSource: TDataSource.  In the components constructor, create the DataSource object, and be sure to free it in the component's destructor.  You can then use the properties and methods of your DataDource property to do whatever you want.

angererAuthor Commented:
I am sorry but this is the stand of information i had before too. The documentation on this problem is not very good (or i am blind) and therefore i do not know how to access the data with this FDataSource.
It would be wonderfull if you have some code example and can help me with that.

Anyway - thank you very much

DI Bernhard Angerer
Yes, It Works!  I'm not sure what you don't understand.

   TAngerer = CLASS( Whatever )
     FDSource: TDataSource;
     CONSTRUCTOR Create(...);    {<---THis is up to you}
     DESTRUCTOR Destroy;   Override;
     PROPERTY DataSource: TDataSource  Read FDSource;


CONSTRUCTOR TAngerer.Create(...):
   FDSource := TDataSource.Create( Self );

DESTRUCTOR TAngerer.Destroy;

Now, use your object....

XX ;= TAngerer.Create(....);
XX.DataSource := SomeTTable;  {Or whatever}

I hope this is more clear!

Ian C.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

angererAuthor Commented:
Thank you very much for your efford but,
let us inherit TAngerer from TEdit and please show me how i can assign the Text property with let as say the NAME column from the table ANIMALS.DBF (example table from delphi). In the text field should than appear 'Angel Fish' when the cursor is standing on the first position.

I'll assume you're trying to create a data-aware control.  

If so, you should check the Delphi docs & source for TFieldDataLink.  This is the class which actually does the data access.  In your class, the Datasource property is just a string.

Example from the source (TDBEdit):

constructor TDBEdit.Create(AOwner: TComponent);
  inherited Create(AOwner);
  inherited ReadOnly := True;
  ControlStyle := ControlStyle + [csReplicatable];
  FDataLink := TFieldDataLink.Create;
  FDataLink.Control := Self;
  FDataLink.OnDataChange := DataChange;
  FDataLink.OnEditingChange := EditingChange;
  FDataLink.OnUpdateData := UpdateData;

function TDBEdit.GetDataSource: TDataSource;
  Result := FDataLink.DataSource;

procedure TDBEdit.SetDataSource(Value: TDataSource);
  FDataLink.DataSource := Value;
  if Value <> nil then Value.FreeNotification(Self);

Clearly there is other code you need.  But not much.  I recommend Ray Konopka's book on developing custom controls, but the docs have a partial example.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I'm sorry, I thought you said that you wanted to use a datasource within a control and perhaps to access a ttable from there.  I didn't know that you wanted to know how to make a control data aware.

Ian C.

angererAuthor Commented:
Thank you...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.