To achieve the synchronization between a TObjectList with class instances and a TStringGrid in Delphi, you can utilize RTTI (Run-Time Type Information) to dynamically map properties to grid columns. This flexible solution allows you to adapt to different class structures without hard-coding property accesses.
Here's a step-by-step approach on how to implement this:
- Define Your Class:
First, define the class whose properties you want to map to the
TStringGrid. Ensure it's equipped with property change notifications.
type
TMyClass = class
private
FName: string;
FAge: Integer;
FOnChange: TNotifyEvent;
procedure SetName(const Value: string);
procedure SetAge(const Value: Integer);
public
property Name: string read FName write SetName;
property Age: Integer read FAge write SetAge;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
procedure TMyClass.SetName(const Value: string);
begin
if FName <> Value then
begin
FName := Value;
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;
procedure TMyClass.SetAge(const Value: Integer);
begin
if FAge <> Value then
begin
FAge := Value;
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;
- Set Up the TObjectList:
var
MyObjectList: TObjectList<TMyClass>;
- Map Properties Using RTTI:
Create a procedure to map each property of the objects to specific columns in the grid using RTTI.
uses
RTTI, TypInfo;
procedure MapObjectListToStringGrid(Grid: TStringGrid; ObjectList: TObjectList<TMyClass>);
var
ctx: TRttiContext;
obj: TMyClass;
rttiType: TRttiType;
prop: TRttiProperty;
col, row: Integer;
begin
ctx := TRttiContext.Create;
try
if ObjectList.Count > 0 then
begin
obj := ObjectList[0];
rttiType := ctx.GetType(obj.ClassType);
// Assume first row is headers
row := 1;
for obj in ObjectList do
begin
col := 0;
for prop in rttiType.GetProperties do
begin
if prop.IsReadable then
begin
Grid.Cells[col, row] := prop.GetValue(obj).ToString;
Inc(col);
end;
end;
Inc(row);
end;
end;
finally
ctx.Free;
end;
end;
- Update TStringGrid when Object Properties Change:
Add a method to update the grid cells whenever a property changes:
procedure UpdateGridForObject(Grid: TStringGrid; ObjectList: TObjectList<TMyClass>;
Instance: TMyClass);
var
Index, col: Integer;
ctx: TRttiContext;
rttiType: TRttiType;
prop: TRttiProperty;
begin
ctx := TRttiContext.Create;
try
Index := ObjectList.IndexOf(Instance);
if Index >= 0 then
begin
rttiType := ctx.GetType(Instance.ClassType);
col := 0;
for prop in rttiType.GetProperties do
begin
if prop.IsReadable then
begin
Grid.Cells[col, Index+1] := prop.GetValue(Instance).ToString;
Inc(col);
end;
end;
end;
finally
ctx.Free;
end;
end;
Each object instance should invoke its OnChange event whenever a property is modified to call UpdateGridForObject.
- Handle Changes from TStringGrid:
To react to cell changes in the grid, capture the OnSetEditText or OnCellClick event of your TStringGrid and update the object property correspondingly:
procedure TStringGridCellChange(Sender: TObject; ACol, ARow: Integer; const Value: stri