MFC Grid manual

How to implement enumeration format?

// This example demonstrates how to present a numeric value (enum) as text in the grid


//MyClass.h file
//Declaration of some C++ class
class CMyClass : public Dapfor::Common::CDataObject
{
public:
    //It is better 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
    {
        ...
        FidCity,
    };

    // A list of numeric identifiers used by CLongEnumFormat.
    // A mapping table of these identifiers and strings 
    // is declared in MyClass.cpp file
    enum City
    {
        London = 100,
        Paris,
        Totonto,
        NewYork,
    };

public:
    CMyClass(City city);
    virtual ~CMyClass();

    //Public methods
    ...
    City    GetCity() const {return m_City;}

private:
    City    m_City;

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


//MyClass.cpp file

// This declaration can be used to display numeric value (enum) as text in the grid
Dapfor::Common::CLongEnumFormat::Item cities[] =
{
    {CMyClass::London,  "London"},
    {CMyClass::Paris,   "Paris"},
    {CMyClass::Totonto, "Totonto"},
    {CMyClass::NewYork, "New York"},
};

// Field map declaration.
// To put an object of CMyClass into the grid it should declare a table 
// that supports calling specified functions by identifiers. 
// We can also customize a presentation using formats included in 
// Common library or custom formats.
DF_BEGIN_FIELD_MAP(CMyClass)
    ...
    DF_ENUM_ID  (FidCity,     "City",     &CMyClass::GetCity,     0, DF_ENUM_FORMAT(cities))
DF_END_FIELD_MAP()


CMyClass::CMyClass(City city) : m_City(city)
{
}

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



//Using sample:


    //grid initialization
    ...

    Dapfor::GUI::CHeader* header = new Dapfor::GUI::CHeader();
    ...
    header->Add(new Dapfor::GUI::CColumn(CMyClass::FidCity,     "City", 100));
    m_Grid.SetHeader(header);

    Dapfor::Common::CDataObject* pDO = new CMyClass(CMyClass::Paris);
    m_Grid.Add(pDO);