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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to make GridControl.ClientSize fit to its content?

Thread ID:





24281 Feb 1,2005 12:37 AM UTC Feb 1,2005 07:11 AM UTC WinForms 1
Tags: GridControl
Asked On February 1, 2005 12:37 AM UTC

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!

Administrator [Syncfusion]
Replied On February 1, 2005 07:11 AM UTC

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);


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon