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
close icon

ResizeToFit Question

I just want to confirm that to resize the columns on load to fit the max column value size I will need to loop through each value in the column. I will need to do this for all columns in the grid. Note I am loading this from a database. I have tried your sample; Try Me.GridDataBoundGrid1.BeginUpdate() 'we'll read the data from a local XML file 'comment this line if you want to access your SQLserver ReadXml("Data\GDBDdata.XML") 'uncomment this Fill line if you want to access your SQLserver 'Me.SqlDataAdapter1.Fill(Me.DataSet11) Me.GridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Row(0)) Me.GridDataBoundGrid1.CurrentCell.MoveTo(1, 1) Me.GridDataBoundGrid1.EndUpdate() Catch ex As Exception MessageBox.Show("This sample needs to access SQL server. Please recheck the connection string.") MessageBox.Show(ex.ToString()) End Try and it seems as though it only resizes based on column title. Is there a faster way to resize all columns bases on field value?

1 Reply

AD Administrator Syncfusion Team November 19, 2002 04:06 PM UTC

Unless you have special knowledge of the contents of a column, then you will have to use code that somehow looks at each cell to size the cell. For example, if you know a column can only have a 5 digit integer, then you can just explicitly set the column width to 5 * average character width for the font you are using. This is what I mean by special knowledge of the column contents. The ColWidths.ResizeToFit method uses the range that is passed in to decide what cells it will use to size the colwidths. So, in the code you listed, it uses GridRangeInfo.Row(0), which is row 0, or the column header row. That is why that code sizes the cells only based on the column header. If you pass it GridRangeInfo.Table, then it will size things based on the whole table. If you pass it, GridRangeInfo.Cols(2), itr will size column 2, and so on. So, if your table is not too large, calling Colwidths.ResizeToFit passing the whole table may be reasonable. But if your table is huge, this may not be an option. In that case, you could try just resizing the visible portion by restricting the range between the grid.TopRowIndex and the grid.ViewLayout.LastVisibleRow. Or, you could use special knowledge to size things without having to look at the contents of every cell.

Live Chat Icon For mobile
Up arrow icon