I have a large grid with many rows and I would like to display rows with optimal height. How can I delay resizing the rows so that the row heights are only calculated before the row gets displayed?
Please add the following declarations to your view
virtual int GetRowHeight(ROWCOL nRow);
virtual long OnCalcRowHeightOnDemand(ROWCOL nRow);
and the following code in the implementation .cpp file
class CMyParam: public CGXGridParam
{
friend class C1stGridView;
};
int C1stGridView::GetRowHeight(ROWCOL nRow)
{
CMyParam* pParam = (CMyParam*) GetParam();
// if height of this row is already computed
long nHeight;
if (!IsRowHidden(nRow) && !pParam->m_RowHeights.Lookup(nRow, nHeight))
{
pParam->m_RowHeights.SetAt(nRow, Height_DPtoLP(GetDefaultRowHeight()));
nHeight = OnCalcRowHeightOnDemand(nRow);
pParam->m_RowHeights.SetAt(nRow, Height_DPtoLP(nHeight));
}
return CGXGridCore::GetRowHeight(nRow);
}
long C1stGridView::OnCalcRowHeightOnDemand(ROWCOL nRow)
{
// compute height of this row (code copied from ResizeRowHeightsToFit)
CGXStyle* pStyleStandard = &StandardStyle();
CClientDC dc(m_pGridWnd);
OnGridPrepareDC(&dc);
// select default font into device context
CFont* pOldFont = LoadFont(&dc, *pStyleStandard);
int nHeight = 0;
ROWCOL nColCount = GetColCount();
for (ROWCOL nCol = GetHeaderCols()+1; nCol <= nColCount; nCol++)
{
if (GetColWidth(nCol) > 0)
{
CGXStyle* pStyle = CreateStyle();
ComposeStyleRowCol(nRow, nCol, pStyle);
CGXControl* pControl = GetRegisteredControl(pStyle->GetControl());
CSize size = pControl->CalcSize(&dc, nRow, nCol, *pStyle, pStyleStandard, TRUE);
nHeight = max(nHeight, size.cy);
// Style object recycling (CreateStyle will use this style-object again)
RecycleStyle(pStyle);
}
}
if (pOldFont)
dc.SelectObject(pOldFont);
return nHeight;
}
If necessary, you can force that all row heights will be recalculated by emptying
pParam->m_RowHeights.