MFC Grid manual

Declaration of a C++ class that has to be inserted into the grid

// MyClass.h file

// This is a declaration of a class, an instance of which can be directly 
// inserted to the grid. The class has some functions that return values 
// shown in grid columns.  

class CMyClass : public Dapfor::Common::CDataObject
{
public:
    // It is useful to use enumerations instead of long numeric values...
    // The grid can use the same identifiers to show values returned 
    // by functions of this class.
    enum
    {
        FidPrice,
        FidQuantity,
        FidTime,
    };

public:
    CMyClass(double price, __int64 quantity, long time);
    virtual ~CMyClass();

    //Get- methods
    double  GetPrice() const;
    __int64 GetQuantity() const;
    long    GetTime() const;

private:
    double  m_Price;
    __int64 m_Quantity;
    long    m_Time;

    //Declaration of map that contains a list of the functions, 
    //that can be called by their identifiers.
    DF_DECLARE_FIELD_MAP();
};




//MyClass.cpp file

//Some constructor...
CMyClass::CMyClass(double price, __int64 quantity, long time)
    : m_Price(price)
    , m_Quantity(quantity)
    , m_Time(time)
{
}

//Virtual destructor
CMyClass::~CMyClass()
{
    //The object ends the life cycle. We notify the remaining subscribers 
    //that can listen to notifications.
    NotifyDelete();
}

// Declaration of a field map.
// To put an object of CMyClass into the grid, it should declare a table 
// that enables calls of specified functions by identifiers. 
// We can also customize its presentation using formats included in Common 
// library or use our custom formats.
DF_BEGIN_FIELD_MAP(CMyClass)
    DF_DOUBLE_ID(FidPrice,    "Price",    &CMyClass::GetPrice,    0, 0)
    DF_INT64_ID (FidQuantity, "Quantity", &CMyClass::GetQuantity, 0, 0)
    DF_LONG_ID  (FidTime,     "Time",     &CMyClass::GetTime,     0, new CLongDateFormat(CLongDateFormat::date))
DF_END_FIELD_MAP()


//Implementation of get-functions:
double  CMyClass::GetPrice() const
{
    return m_Price;
}
__int64 CMyClass::GetQuantity() const
{
    return m_Quantity;
}
long CMyClass::GetTime() const
{
    return m_Time;
}





//How to use:

    //Grid initialization
    ...

    Dapfor::GUI::CHeader* header = new Dapfor::GUI::CHeader();
    header->Add(new Dapfor::GUI::CColumn(CMyClass::FidPrice,    "Price", 100));
    header->Add(new Dapfor::GUI::CColumn(CMyClass::FidQuantity, "Quantity", 100));
    header->Add(new Dapfor::GUI::CColumn(CMyClass::FidTime,     "Time", 100));
    m_Grid.SetHeader(header);

    ...

    CMyClass* obj1 = new CMyClass(12.34, 100, Dapfor::Common::CLongDateFormat::Now());
    m_Grid.Add(obj1);
    
    ...