سؤال

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.

هل كانت مفيدة؟

المحلول

The Quantum Grid has three different ways to access data. It can work unbound (you access directly the cells), bound (the standard way of using a datasource) or in "provider" mode, where you have to write the proper class (the provider) to access and modify data. In provider mode, the data source can be whatver you like. The help details how to implement a provider. THere should also be an UnboundListDemo among the demo applications.

نصائح أخرى

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top