tags:

views:

429

answers:

1

I've got an in-memory dataset with several fields, one of which is a primary key that another dataset references as a foreign key. Thing is, the master dataset can have multiple references to the detail dataset. (This is modeling an object that contains a dynamic array of other objects.)

If there was only one of each sub-object, I could make the right association with the KeyFields and LookupKeyFields properties of the reference field in the master dataset, but that's only designed to return one result. I want to load all the records whose primary key matches the right ID key and display them in a listbox.

I thought a TDBListBox would help with this, but it turns out that's not what they do. So how would I populate a listbox or similar control with the result set of a multiple-match check like that for further editing? I want something similar to the result of a SQL query like this:

select field1, field2, field3
from client_dataset
where client_dataset.primary_key = master_dataset.id

Only thing is, this is done entirely with in-memory datasets. No real databases are being used here. Does anyone know how this can be done?

+3  A: 

The dataset has a Filter property which can be set with a condition. You also have to set the filtered flag on true. And with he datacontrols you can select which fields are visible.

So:

var
  c : TColumn;
begin
  clientdataset.Filter := Format('primary_key = %d', [master_dataset.id]);
  clientdataset.Filtered := True;

  c := DBGrid1.Columns.Add;
  c.FieldName := 'field1';
  c := DBGrid1.Columns.Add;
  c.FieldName := 'field2';
  c := DBGrid1.Columns.Add;
  c.FieldName := 'field3';
end;

Should do the trick.

Gamecat
Good answer. The dataset I was using doesn't currently implement the Filter property, or I'd have probably found it on my own, but now that you say that I remember seeing a discussion on their newsgroup about a new patch that makes it work. I'll have to check it out.
Mason Wheeler