Hi Greg,
Using the TwwDataGrid without a physical database
If you want your grid bound to data in-memory instead of a physical file or database, then there are a number of ways you can achieve this with the TwwDataGrid. This completely frees you from having to create a physical file .
One way to go about this is to use the native control TClientDataSet (included with Delphi) as an in-memory dataset, and then link it to the InfoPower grid. You can define its structure, add fields, and add data, all without using any physical file. It also requires very little effort to setup. The advantages become enormous once you have done this, as you can then filter your dataset, search for records, sort, etc. all without having to write the code for this functionality. TClientDataSet also is supported on all Delphi FireMonkey platforms, including the mobile space (Android and iOS).
Or, attach this code to create an in-memory dataset, add records, and display its contents. You can attach our grid to such a in-memory dataset. You can also do this at design time or with code as the steps below indicates.
The following summarizes the steps in using a TClientDataSet for an in-memory manager of records.
1. Create a TClientDataset instance. You may do it in design-time (simply drop a component on form) or at run-time (for example, in the OnCreate event of your form):
var InMemoryDataSet: TClientDataSet; begin InMemoryDataSet := TClientDataset.Create(Application);
2. Now add field definitions (FieldDefs property) using the IDE designer or with code such as the following.
InMemoryDataSet.FieldDefs.Add('ID', ftInteger, 0, False);
InMemoryDataSet.FieldDefs.Add('Status', ftString, 10, False);
InMemoryDataSet.FieldDefs.Add('Created', ftDate, 0, False);
InMemoryDataSet.FieldDefs.Add('Volume', ftFloat, 0, False);
3. Create the dataset in the IDE by right-clicking the component or use code like the following.
InMemoryDataSet.CreateDataset
4. Open the dataset
InMemoryDataSet.Active:= true;
5. You're done. Now you can use the TDataSet methods to manipulate your in-memory data. You can add, edit, or delete records using the TDataset methods. You can filter using the Filter property, sort using the IndexFieldName property, search using the Locate method, etc.
For example, to add random values to records:
for i := 1 to 100 do
begin
InMemoryDataSet.Append;
InMemoryDataSet.FieldByName('ID').AsInteger := i;
InMemoryDataSet.FieldByName('Status').AsString := 'Code'+IntToStr(i);
InMemoryDataSet.FieldByName('Created').AsDateTime := Date();
InMemoryDataSet.FieldByName('Volume').AsFloat := Random(10000);
InMemoryDataSet.Post;
end;
Once you have successfully created this in-memory dataset, you can view it using the TwwDataGrid. Just create a TBindSource pointing to InMemoryDataSet, and set the TwwDataGrid.DataSource property to the TBindSource.
The following code attached to a button, creates a TClientDataSet at runtime and adds 5 records with each record containing the columns 'StringField' and 'IntegerField'
uses Datasnap.DBClient;
procedure TForm45.Button1Click(Sender: TObject);
var
i: integer;
begin
with TClientDataset.Create(nil) do
try
Close;
// Add fields
FieldDefs.Add('StringField', ftString, 10);
FieldDefs.Add('IntegerField', ftInteger);
CreateDataset;
// Add records
for i := 1 to 5 do
begin
AppendRecord([IntToStr(i), i]);
{ or
Insert;
FieldByName('StringField').AsString := IntToStr(i);
FieldByName('IntegerField').AsInteger := i;
Post;
}
end;
First;
while not EOF do
begin
ShowMessage(FieldByName('StringField').AsString);
next;
end;
finally
free;
end;
end;
-Roy
