I'm wondering if it's possible to bind a TList object as a cxGrid datasource.
So what I have is a TList object containing various objects that i do not need to persist. I want a sort of GridView to serve as an overview of "selected items", and the selected items being the objects in the list.
It would be preferable that the columns were to be defined by the object type stored in the TList.
Is this easily doable, and if so would you be able to give me an overview of how this is done. I'm currently using a ListBox that uses tabWidth as a sort of column seperator but would prefer to make the switch.
Let's assume that you have a TList derived class TMyList, that holds items of TMyListItem class. You would then derive from TcxCustomDataSource.
TTListDataSource = class(TcxCustomDataSource)
private
FTList : TMyList;
protected
function GetRecordCount: Integer; override;
function GetValue(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle): Variant; override;
public
constructor Create(ATList : TMyList);
end;
The implementation would be like this:
constructor TTListDataSource.Create(ATList : TMyList);
begin
inherited Create;
FTList := ATList;
end;
function TTListDataSource.GetRecordCount: Integer;
begin
result := FTList.Count;
end;
function TTListDataSource.GetValue(ARecordHandle: TcxDataRecordHandle;
AItemHandle: TcxDataItemHandle): Variant;
var
aIndex : Integer;
aMyListItem : TMyListItem;
begin
aCurrentIndex := Integer(ARecordHandle);
if (aCurrentIndex > -1) and (aCurrentIndex < FTList.Count) then begin
aMyListItem := FTList[aCurrentIndex)] as TMyListItem;
aIndex := Integer(AItemHandle);
case aIndex of
0 : result := '';
1 : result := aMyListItem.Year;
2 : result := aMyListItem.Quarter;
end
else
result := '';
end;
And you would use your class:
FTListDataSource := TTListDataSource.Create(ATList);
ThePivotGrid.DataController.CustomDataSource := FTListDataSource;
FTListDataSource.DataChanged;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With