MFC Grid manual

How to create the grid in the Dialog-based application?

It is very easy to use the grid in a dialog-based application. See below for more details.

1. Create a dialog-based application.

2. Configure the application as shown in How to install the MFC grid and compile the first application?

3. Open the Control Panel, select a Custom Control and put this control to the dialog form. Then type Dapfor.Grid in the 'Custom Control Properties' window in the 'Class' edit box as shown below:

grid_in_dialog.gif

4. Add a member of CGrid to the dialog

//DialogApplicationDlg.h file 

#pragma once
#include <Dapfor/GUI/Grid.h>

//Declaration of a dialog class
class CDialogApplicationDlg : public CDialog
{
...

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

private:
    Dapfor::GUI::CGrid m_Grid; //The grid will register 'Dapfor.Grid' window class name in the constructor.

};

5. Implement DoDataExchange mechanism for the grid control

void CDialogApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDialogApplicationDlg)
    DDX_Control(pDX, IDC_CUSTOM_GRID, m_Grid);
    //}}AFX_DATA_MAP
}

6. The dialog is ready. Now you can add some columns to the header and configure the grid in the OnInitDialog() handler.

BOOL CDialogApplicationDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    
    //The grid should be initialized here after DDX exchange.   


    //Now, let's create a header and add some columns
    Dapfor::GUI::CHeader* header = new Dapfor::GUI::CHeader(true);
    header->Add(new Dapfor::GUI::CColumn(CTestClass::FidName, "Name", 100));
    
    //Set header for the grid
    m_Grid.SetHeader(header);

    return TRUE; 
}