How to define a TVirtualStringTree with dynamic data structure
I would like to dynamically load and display into a TVirtualStringTree
, data from some different SQL tables. This mean that the header and the content of each column will contain each time different type of data.
My problem is how to optimize in regards of memory usage, the definition of the record and the pointer for this case.
My thinking was to do it like this:
type
TDataType = (dtUnknown, dtString, dtInteger, dtText, dtFloat, dtDateTime, dtDate, dtTime, dtBoolean);
TData = record
DataType: TDataType;
AsString: String;
AsInteger: Integer;
AsText: TStrings;
AsWord: Word;
AsDateTime: TDateTime;
AsDate: TDate;
AsTime: TTime;
AsBoolean: Boolean;
end;
TTreeData = array of TData;
PTreeData= ^TTreeData;
In practice only 2 fields from the record will contained data: DataType
(all the time) and a second field, depends by the DataType
defined (eg AsString
, AsInteger
). Will be also the others fields allocated as memory when Node will be initialized? Also I don't like the fact the DataType
is allocated to each node. There must be a simple way to optimize this record.
Please some suggestions.
When I need to do something like this I use a case statement in the TData record.
Something like
type
TData = record
case DataType: TDataType of
dtUnknown:
(AsUnknown: ???);
dtString:
(AsString: string); // this is incorrect, string (and some others are not allowed)
dtInteger:
(AsInteger: Integer);
... and so on
end;
end;
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Structured_Types#Variant_Parts_in_Records
Have a look at TVarRec. Delphi uses open arrays TVarRec when implementing 'array of const' variables ans in the Format statement.
You probably only need two columns in the VirtualStringTree. One for the type and other for the value (represented as a string)
链接地址: http://www.djcxy.com/p/35032.html上一篇: 在数据库中实现分层数据结构