Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS General Discussion » Autosizing Columns in grid control Collapse All
Subject Author Date
ernest peters Nov 12, 2004 - 12:35 AM

is there a way to do this? as cells are entered i would like the control to autosize to the biggest entry..

Technical Support Nov 12, 2004 - 3:32 AM

To automatically adjust the column width in the CExtGridWnd control, please use the following extension:

 
class CMyGridWnd : public CExtGridWnd
{
public:
 
 void DoAutoSizeColumn( LONG nColNo )
 {
  ASSERT_VALID( this );
 
  CExtGridCellHeader * pHeaderCell =
   STATIC_DOWNCAST(
    CExtGridCellHeader,
    GridCellGetOuterAtTop( nColNo, 0L )
    );
  INT nWidthCalc, nWidthMax;
  pHeaderCell->OnQueryExtent( nWidthCalc, true, -1 );
  pHeaderCell->OnQueryExtent( nWidthMax, true, 1 );
  { // block
   CClientDC dc( this );
   LONG nRowCount = RowCountGet();
   for( LONG nRowNo = 0L; nRowNo < nRowCount; nRowNo++ )
   {
    CExtGridCell * pCell =
     GridCellGet( nColNo, nRowNo );
    if( pCell == NULL )
     continue;
    CSize sizeReqired =
     pCell->OnMeasureTextSize(
      *this,
      dc,
      0L,
      0L,
      nColNo,
      nRowNo,
      0L,
      0L,
      __EGBWA_INNER_CELLS
      );
    if( nWidthCalc < (sizeReqired.cx+7) )
     nWidthCalc = (sizeReqired.cx+7);
    if( nWidthCalc >= nWidthMax )
     break;
   }
  } // block
  if( nWidthCalc > nWidthMax )
   nWidthCalc = nWidthMax;
  pHeaderCell->ExtentSet( nWidthCalc );
  OnSwUpdateScrollBars();
  OnSwDoRedraw();
 }
 
 virtual bool OnGbwAnalyzeCellMouseClickEvent(
  UINT nChar, // VK_LBUTTON, VK_RBUTTON or VK_MBUTTON only
  UINT nRepCnt, // 0 - button up, 1 - signle click, 
                // 2 - double click, 
                // 3 - post single click & begin editing
  UINT nFlags, // mouse event flags
  CPoint point // mouse pointer in client coordinates
  )
 {
  ASSERT_VALID( this );
  if( CExtGridWnd::OnGbwAnalyzeCellMouseClickEvent(
    nChar,
    nRepCnt,
    nFlags,
    point
    )
   )
   return true;
  if( nRepCnt == 2 )
  {
   CExtGridHitTestInfo htInfo( point );
   HitTest( htInfo, true, true );
   if(  (! htInfo.IsHoverEmpty() )
    && htInfo.IsValidRect()
    && htInfo.GetInnerOuterTypeOfColumn() == 0
    && htInfo.GetInnerOuterTypeOfRow() < 0
    && (htInfo.m_dwAreaFlags & __EGBWA_NEAR_CELL_BORDER_H) != 0
    )
   {
    //TRACE1( "click on column %d\n", htInfo.m_nColNo );
    DoAutoSizeColumn( htInfo.m_nColNo );
    return true;
   } // if( (! htInfo.IsHoverEmpty() ) ...
  } // if( nRepCnt == 2 )
  return false;
 }
 virtual bool OnGbwBeginEdit(
  LONG nVisibleColNo,
  LONG nVisibleRowNo,
  LONG nColNo,
  LONG nRowNo,
  INT nColType,
  INT nRowType,
  const RECT & rcCellExtra,
  const RECT & rcCell,
  const RECT & rcInplaceControl,
  bool bContinueMsgLoop = true
  )
 {
  ASSERT_VALID( this );
  if( ! CExtGridWnd::OnGbwBeginEdit(
    nVisibleColNo,
    nVisibleRowNo,
    nColNo,
    nRowNo,
    nColType,
    nRowType,
    rcCellExtra,
    rcCell,
    rcInplaceControl,
    bContinueMsgLoop
    )
   )
   return false;
  DoAutoSizeColumn( nColNo );
  return true;
 }
};

The CMyGridWnd class autosizes columns when the column separator is clicked or cell editing is finished. You may also invoke its DoAutoSizeColumn method if you need to resize a column according to the content of all cells in this column.

ernest peters Nov 17, 2004 - 6:50 PM

Hi Guys,
Thanks for the response..the code works fine except for one thing..i lose my pre-formatting on the cell (combo boxes etc). I know a simple solution would be to add column data -> auto size column -> format column data, but this would mean i would need to invoke the auto size routine in about half a dozen places whereas i would like to add ALL column data -> format ALL columns -> auto size GRID
Can this be done easily?

Technical Support Nov 18, 2004 - 3:46 AM

We also believe that it is possible to invoke the column auto size code in fewer places. But we do not know how your code works. If you modify the grid data in two dozen places, then you have to invoke the autosize code two dozen times.