MFC Grid manual

How to implement a custom format?

//Values returned by C++ objects and inserted into the grid, can be 
//presented in cells in various ways. The type of presentation depends 
//on format that transforms value to formatted string and vice versa.
//Below you can see example of implementing a custom format. This format 
//can be set in the mapping table or in the Dapfor::GUI::CColumn class.

//CustomLongFormat.h file

//Custom format that transforms long value to a string and vice versa
class CCustomLongFormat : public Dapfor::Common::CFormat
{
public:
    CCustomLongFormat();

    //Pair of format and parse functions
    virtual std::string Format(long val, const Dapfor::Common::CDataObject* pDO) const;
    virtual bool Parse(const std::string& str, long& val, const Dapfor::Common::CDataObject* pDO) const;

    //An object of this class can be cloned.
    virtual Dapfor::Common::CFormat* Clone() const;
};



//CustomLongFormat.cpp file

//Constructor. Format type is Dapfor::Common::Long
CCustomLongFormat::CCustomLongFormat() : Dapfor::Common::CFormat(Dapfor::Common::Long)
{
}

//Format implementation -> transforms a long value to a string
std::string CCustomLongFormat::Format(long val, const Dapfor::Common::CDataObject* pDO) const
{
    switch(val)
    {
        case 0: return "Zero";
        case 1: return "One";
        case 2: return "Two";
    }
    return "";
}

//Parse implementation -> transforms a string to a long value
bool CCustomLongFormat::Parse(const std::string& str, long& val, const Dapfor::Common::CDataObject* pDO) const
{
    if(str == "Zero") 
    {
        val = 0;
        return true;
    }
    if(str == "One") 
    {
        val = 1;
        return true;
    }
    if(str == "Two") 
    {
        val = 2;
        return true;
    }
    return false;
}

//Clone implementation
Dapfor::Common::CFormat* CCustomLongFormat::Clone() const
{
    return new CCustomLongFormat();
}


//How to use the format:
DF_BEGIN_FIELD_MAP(CMyClass)
    ...
    DF_LONG_ID  (MyFid,     "Custom format",     &CMyClass::GetMyLong,   0,  new CCustomLongFormat())
    ...
DF_END_FIELD_MAP()