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

Resize column width to fit widest column (html table style)

I am using the virtual grid and I need to handle the size of my columns. My customer would prefer that the table columns is just the size of the widest text in the column. Pretty much like a html table works. Right now I handle the QueryColWidth something like this for the dynamically resized columns: e.Size = longestText.Length * cellFont.Height; Where the longestText is a result of a search for the longest text in the column. Hope you don't mind that all my questions come at once. I have been using the Grid for about three months and I have these final issues before the customer will accept my delivery. Thanks again.

5 Replies

AD Administrator Syncfusion Team August 22, 2003 05:46 AM UTC

Did you mean to type cellFont.Width? Is this not working for you? Another way you can do this is to call grid.ColWidths.ResizeToFit(GridRangeInfo.Table(), GridResizeToFitOptions.None); at some point after you have subscribed to your virtual grid event handlers. Now, if your table is large, this may take some time as it visits each cell dynamically computing the size required using Graphics.MeasureString. So, in that case, your solutiion might be better.


JI Jimmy August 22, 2003 05:57 AM UTC

There is no Width property. It is a proportional font (verdana). The height returns the height of the tallest letter, so multiplying that by the length of a string and hoping to get a good result is a long shot - A hack even. I think I am going to try a MeasureString on the longestText string I have. Good idea. You should consider getting a html mode in some future version. Thanks.


AC Aaron Ching August 23, 2003 08:36 PM UTC

Hi, I have problem with similar issue. I have a GridControl (instead of a DataBound one) and when I double-click on the header border, it shrinks automatically to a fixed width. Is there some properties I need to set in order to have the double-click auto-resize to the width of the max string length? So far, all ResizeToFit samples are referring to a DataBoundGrid. Is there an event on that header-border-double-click so I can call the ResizeToFit from there? Many thanks as usual. Aaron


AD Administrator Syncfusion Team August 24, 2003 08:07 AM UTC

In a GridControl, when you double-click a column border, it resets the row height back to the default size, GridControl.DefaultColWidth. It does not autosize the column. If you want to autosize the column on a doubleclick, you can catch the ResizingCols event, and autosize things yourself. Below is some code.
’ add the handler
AddHandler Me.GridControl1.ResizingColumns, Addressof GridResizingColumns
.....
Private Sub GridResizingCollumns(sender As Object, e As GridResizingColsEventArgs)
	If e.Reason = GridResizeCellsReason.DoubleClick Then
	Me.GridControl1.ColWidths.ResizeToFit( Me.GridControl1.ViewLayout.VisibleCellsRange.IntersectRange(e.Cols), False, True)
		e.Cancel = True
	End If
End Sub ’GridResizingColumns


AC Aaron Ching August 25, 2003 09:50 PM UTC

Thanks very much as always.

Loader.
Live Chat Icon For mobile
Up arrow icon