Objective Grid: Displaying rows with optimal height

Article ID: 717
Last updated: 11 Jun, 2018
Article ID: 717
Last updated: 11 Jun, 2018
Revision: 3
Views: 1956
Posted: 12 Jan, 2001
by Meltreger B.
Updated: 11 Jun, 2018
by Meltreger B.

Problem


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?


Cause



Action


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.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 717
Last updated: 11 Jun, 2018
Revision: 3
Views: 1956
Posted: 12 Jan, 2001 by Meltreger B.
Updated: 11 Jun, 2018 by Meltreger B.

Others in this category