1stGrid - Step 3
Objective Grid stores all document-specific attributes in the parameter object. When you store the parameter object in the document, all grid views connected to the document can access the same data. In OnInitialUpdate() you will see how to pass a pointer to the parameter object to the grid.
Modify the 1stGrid Document's Header File
This step adds a CGXParam object to the application's CDocument class. This enables all grid views to share the same parameter object.
1. Open 1stgrdoc.h
2. Add a public member variable to the attributes section of C1stGridDoc:
CGXGridParam* m_pParam;
Modify the 1stGrid Document's Implementation File
It is good practice to add clean-up code in OnNewDocument(). This makes it easier to use the document in a single document interface (SDI) application later.
1. Open 1stgrdoc.cpp
2. Replace the C1stGridDoc constructor with the following version:
 
C1stGridDoc::C1stGridDoc()
{
m_pParam = NULL;
}
3. Replace the C1stGridDoc destructor with the following version:
 
C1stGridDoc::~C1stGridDoc()
{
delete m_pParam;
}
4. Change the C1stGridDoc::Serialize() method as follows:
 
void C1stGridDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// Store parameter-object
ar << m_pParam;
}
else
{
// Create parameter-object
ar >> m_pParam;
}
}
5. Modify C1stGridDoc::OnNewDocument() as follows:
 
BOOL C1stGridDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
 
// Clean up parameter-object
delete m_pParam;
m_pParam = NULL;
 
return TRUE;
}
Modify the 1stGrid View's Implementation File
In your OnInitialUpdate() method you need to pass the pointer to the parameter object to the grid object. If the first view for a document is opened, the parameter object must be allocated and the grid data initialized. If the end user opens additional views for the same document, no initialization is necessary. The grid data is already available in the parameter object.
bFirstView = TRUE indicates that this is the first view connected to the document and therefore the data needs to be initialized.
Next, construct the parameter object and pass the pointer to the grid view.
Your existing initialization code should be bracketed with an if statement where you should check if bFirstView is TRUE.
1. Add the following code to the beginning of C1stGridView::OnInitialUpdate():
 
BOOL bFirstView = FALSE;
 
if (GetDocument()->m_pParam == NULL)
{
bFirstView = TRUE;
GetDocument()->m_pParam = new CGXGridParam;
}
 
SetParam(GetDocument()->m_pParam, FALSE);
2. Enclose the existing initialization code with an if statement that checks for bFirstView = TRUE:
if (bFirstView)
{
// ... (Existing init code)
}
3. Please refer to CMy1stGridView::OnInitialUpdate() in <stingray-installdir>\Samples\Grid\Tutorial\1stGrid\Step3 for a complete listing of these changes.
At this point, you can compile and run with these modifications. You can now serialize data into a document and open new views for the same data using Window|New Window. In the next section we will add support for splitting the view.
Add a Splitter Frame Class for 1stGrid
Next, override the OnCreateClient() method:
1. Select View|Class View.
2. Right click on 1stGrid and select Add, then Class.
The Add Class dialog box appears.
3. Select MFC in the Templates section and MFC Class in the Categories section. Then click the Add button.
The MFC Add Class Wizard appears.
4. In the MFC Add Class Wizard, enter CSplitterMDIChildWnd for the Name.
5. Specify CMDIChildWnd as the Base class.
6. Click Finish on the MFC Add Class Wizard dialog.
7. In the Class View, right click on the newly generated class and select Properties.
8. Click on the Overrides icon.
9. Select the OnCreateClient event.
10. Select <Add> OnCreateClient.
11. The function OnCreateClient(...) is added to CSplitterMDIChildWnd.
12. Edit the code in the file SplitterMDIChildWnd.cpp, changing OnCreateClient(…) as follows:
 
BOOL CSplitterMDIChildWnd::OnCreateClient(LPCREATESTRUCT
/*lpcs*/, CCreateContext* pContext)
{
return m_wndSplitter.Create(this,
2, 2,
CSize(10, 10),
pContext);
}
13. Open SplitterMDIChildWnd.h.
14. Add a member variable to the attributes section of CSplitterMdiChildWnd:
 
CSplitterWnd m_wndSplitter;
15. Close and save SplitterMDIChildWnd.h and SplitterMDIChildWnd.cpp.
Change the 1stGrid View to the Splitter Frame Class
1. Open 1stgrid.cpp.
2. After #include "1stgrvw.h”, add:
 
#include "SplitterMDIChildWnd.h"
3. Locate:
 
pDocTemplate = new CMultiDocTemplate(
IDR_1STGRITYPE,
RUNTIME_CLASS(C1stGridDoc),
RUNTIME_CLASS(CMDIChildWnd),// standard MDI child frame
RUNTIME_CLASS(C1stGridView));
AddDocTemplate(pDocTemplate);
and replace CMDIChildWnd with CSplitterMDIChildWnd:
 
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_1STGRITYPE,
RUNTIME_CLASS(C1stGridDoc),
RUNTIME_CLASS(CSplitterMDIChildWnd),
// splitter child frame
RUNTIME_CLASS(C1stGridView));
AddDocTemplate(pDocTemplate);
Compile and Run 1stGrid - Step 3
At this point, you can compile and run the 1stGrid application.
1. Compile the application and run it.
Figure 73 – 1stGrid-Step 3 in action
Congratulations on completing the 1stGrid tutorial! For more information, try these other tutorials: