Resize to fit

Hi guys, I want to put each character in a string into columns in gridControl, then i want the col size (width) fit the charater. so it will look like a string in a column only, currently i''m doing like this: Syncfusion.Windows.Forms.Grid.GridModel gridModel = this.gridControl.Model; gridModel = this.gridControl.Model; gridModel.ColWidths.ResizeToFit (GridRangeInfo.Cells(1, 1, gridModel.RowCount, gridModel.ColCount), GridResizeToFitOptions.None); but col size is still not really fit the col size, there still a white space there, please see an atachment

grid3.zip

1 Reply

AD Administrator Syncfusion Team January 16, 2006 03:03 PM UTC

Hi Akamal, When ResizeToFit method is called with a particular range of cells, Grid calls CellModel.OnQueryPreferredClientSize for all the cells in the range. This method returns the Size for the string. By default grid uses .NET graphics MeasureString method. //From GridModelBase.cs /// /// Calculates the preferred size of the cell based on its contents without margins and any buttons. /// /// The context of the canvas. /// The row index. /// The column index. /// The object that holds cell information. /// /// The optimal size of the cell. protected virtual Size OnQueryPrefferedClientSize(Graphics g, int rowIndex, int colIndex, GridStyleInfo style, GridQueryBounds queryBounds) { return WinFormsUtils.MeasureSampleWString(g, style.GdipFont); } /// /// Gets the preferred size to be used for an empty cell. /// /// The context of the canvas. /// The to be used. /// The of the string "Wg;". public static Size MeasureSampleWString(Graphics g, Font font) { return g.MeasureString(MeasureEmptyCellString, font).ToSize(); } You can see MeasureString returns these widths. Here is a CP article discussing about the incorrect width and provides two solutions to get the correct width. http://www.codeproject.com/cs/media/measurestring.asp Also refer this Grid KB that uses this approach to get the string width to draw. http://www.syncfusion.com/support/kb/grid/Default.aspx?ToDo=view&questId=24 To get the ResizeToFit to include this width calculation, you need to override the OnQueryPrefferedClientSize and pass in the measured width. Refer to sample for details Regards, Madhan.

CharacterDemo_39677.zip

Loader.
Up arrow icon