We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to make GridControl.ClientSize fit to its content?

I have a grid control of which RowCount and ColCount are determined during runtime. How can I custimize Grid''s height and width so that: * There''s no VScrollBar (I''m sure of the maximum row count is less then it''s container control) * The client size width just fits the column size and count if they are small. And HScrollBar appears only when the RowCount exceeds some limits. I have written the following stupid implementation and it swings between nicely fitting and more-or-less unfitting. public int MaxHeight { get { int maxh = m_dataGridChannel.Model .RowHeights [0] + m_dataGridChannel.Model .RowHeights [1] * m_arrListChannelCode.Count ; maxh += ( m_dataGridChannel.HScroll ? 17:4); maxh += ( this.Size .Height - m_dataGridChannel.Size .Height ) ; return maxh; } } public int MaxWidth { get { int maxw = 0; for ( int col = 0; col <= m_frozencount ; col++) maxw += m_dataGridChannel.Model .ColWidths [col]; maxw += m_dataGridChannel.DefaultColWidth * m_htPassnameSel.Count ; maxw += ( m_dataGridChannel.VScroll ? 17 : 4); maxw += ( this.Size .Width - m_dataGridChannel.Size .Width ); return maxw; } } Thanks a lot!

1 Reply

AD Administrator Syncfusion Team February 1, 2005 02:11 AM

Try this code: //total height int h = this.grid.Model.RowHeights.GetTotal(0, this.grid.Model.RowCount + 1); //width of, say, 5 columns int w = this.grid.Model.ColWidths.GetTotal(0, 5); this.grid.ClientSize = new Size(w, h); If grid is a GriddataBoundGrid in the above code, there is some autosizing that is done at the initial display. If you want to control the size of the grid based on colwidths, then this autosizing will throw your calculations off. So, you would need to turn this off using grid.AllowAutoSize = false. You could also call grid.Model.ColWidths.ResizeToFit yourself if you still wanted the autozing after turning off the automatic autosizing at initial display, but you would want tto do it before the above code. this.gridDataBoundGrid1.AllowResizeToFit = false; this.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Table(), GridResizeToFitOptions.IncludeHeaders); //total height int h = this.gridDataBoundGrid1.Model.RowHeights.GetTotal(0, this.gridDataBoundGrid1.Model.RowCount + 1); //width of, say, 5 columns int w = this.gridDataBoundGrid1.Model.ColWidths.GetTotal(0, 5); this.gridDataBoundGrid1.ClientSize = new Size(w, h);

Loader.
Live Chat Icon For mobile
Up arrow icon