Can I resize the client area of a grid view to fit the width or height of the grid?
Yes, the following function will calculate the grids width and height and resize the parent frame accordingly. You can call this function from your OnInitialUpdate routine.
void C1stGridView::FitToSize()
{
UpdateFontMetrics();
CFrameWnd* pFrame = GetParentFrame();
CMDIChildWnd* pMDIChild = NULL;
CWnd* pMDIClient = NULL;
CRect oClientRect;
if (pFrame && pFrame->IsKindOf(RUNTIME_CLASS(CMDIChildWnd)))
{
pMDIChild = (CMDIChildWnd*) pFrame;
pMDIClient = pFrame->GetParent();
pMDIChild->MDIRestore();
pMDIClient->GetClientRect(&oClientRect);
int colTotalWidth = CalcSumOfColWidths(0,GetColCount(), oClientRect.Width());
int colTotalHeight = CalcSumOfRowHeights(0,GetRowCount(), oClientRect.Height());
//check if the calculated value is more than the default client area of the grid
if (colTotalHeight >= oClientRect.Height())
colTotalWidth += ::GetSystemMetrics(SM_CXVSCROLL);
colTotalWidth += 7*(::GetSystemMetrics(SM_CXEDGE));
if (colTotalWidth >= oClientRect.Width())
colTotalHeight += ::GetSystemMetrics( SM_CYHSCROLL);
colTotalHeight += 7*(::GetSystemMetrics(SM_CYEDGE));
//add the window title bar to the height
colTotalHeight+=::GetSystemMetrics(SM_CYCAPTION);
colTotalHeight = min(colTotalHeight, oClientRect.Height());
colTotalWidth = min(colTotalWidth, oClientRect.Width());
pFrame->SetWindowPos(NULL, 0, 0, colTotalWidth,colTotalHeight,
SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
}