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

ResizeToFit ?

Hi All, I'm currently using the dbg to hold messages from a chat program. A row is added to the grid everytime a new message arrives. The grid is divided up into the following columns... time | from | to | message The from, to and message columns have varying length values and I'd like those columns to resize themselves to fit the data in the cell. I haven't been able to figure out the ResizeToFit method yet and I'm kind of lost here. Any Ideas? Thanks in advance for any help. Mike Gorgone

9 Replies

AD Administrator Syncfusion Team October 15, 2002 05:54 PM UTC

You would have to call the ResizeToFit each time you add a new row. So if your new row index in nRow, you could use code such as: this.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Row(nRow), GridResizeToFitOptions.None); You would alos probably have to call ResizeToFit initially (say in the Form_Load event) to size things originally. > Mike Gorgone


AD Administrator Syncfusion Team October 16, 2002 01:38 PM UTC

You might want to use the GridResizeToFitOptions.NoShrinkSize option instead of the None option to avoid the size shrinking with a 'smaller' row.


AM Anthony Mansfield October 31, 2002 05:52 PM UTC

A followup to the original question. On a databound grid, If I set the AllowResizeToFit to true on the grid, should it resize to fit the contents when data is bound? Is this affected if the column widths are set manually prior the data being bound? How can I bind the data and have each of the columns resized to fit the width of the data? Thanks - Anthony


AD Administrator Syncfusion Team October 31, 2002 08:49 PM UTC

The AllowResizeToFit property only affects the resizing of the columns so the the headers fit. It does not take into effect the actual column contents. One way you can make sure all the text fits is to call ResizeToFit on the table at the end of your Form_Load after the DataSource has been set. this.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Table, GridResizeToFitOptions.None);


AM Anthony Mansfield November 1, 2002 06:09 PM UTC

Thanks Clay. That works great and saves me a lot of trying to adjust the column widths. Anthony > The AllowResizeToFit property only affects the resizing of the columns so the the headers fit. It does not take into effect the actual column contents. > > One way you can make sure all the text fits is to call ResizeToFit on the table at the end of your Form_Load after the DataSource has been set. > > this.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Table, GridResizeToFitOptions.None); >


JS John Sandberg November 1, 2002 08:25 PM UTC

I'm trying to get ResizeToFit() working on a bound grid of mine. The grid is holding 2000 records with 12 fields of data each. When I call ResizeToFit() from in my "click" even, it takes more than 10 seconds to refresh the grid, and the columns are not even properly resized. Any ideas on what I am doing wrong? Thanks, John


AD Administrator Syncfusion Team November 1, 2002 10:30 PM UTC

If you want to ResizeToFit 24,000 strings, it may take 10 seconds. You might try calling grid.BeginUpdate before your resizetofit call and grid.EndUpdate after your call, followed by a grid.Refresh call to make sure everything is redrawn properly. Also, make sure you don't have Undo buffer enabled as this will slow things done. But, you may want to consider just resizing the visible grid. This should take the 10 secs down to no secs. The grid.TopRowIndex and grid.ViewLayout.LastVisibleRow will give you access to the visible rows to pass into ResizeToFit.


JS John Sandberg November 4, 2002 01:05 PM UTC

I think I get it now. I was trying to figure out why manually d-clking on the header boarder would go so much faster; that's because it's only resizing to visible cell contents. Unfortunately. My users won't like that very much. Is there a way to only do column widths or any backdoor trick for making auto-widening of columns any faster (either bound or unbound)? I have to admit that 10 seconds isn't really industry standard here. Just about every other grid I've tested can do 10k cells in .3 secs or less. I appologize on bearing down on this point; it just ends up being a big thing for the product I am developing. John


AD Administrator Syncfusion Team November 4, 2002 02:31 PM UTC

John, Currently, Essential Grid visits every cell in the range to be resized, and calls MeasureString to compute exact sizes. If you want to speed things up, I think you will have to either reduce the number of cells being visited, or change the way the size is being computed. For example, if you know a column has a int in it, then you can easily come up with a max size for that column just by multiplying the average character size times the number of digits of the largest value you expect in the column. So, instead of visiting every cell in the column, you could do a quick calculation to set the column widths. Similarly, if it is a string column, and you know the max characters you expect, then you could calculate the maximum width, and not rely on visiting every cell. In such cases, the resize would be instantaneous. Or, if you are populating the grid directly, as opposed to binding to an external datassource, you could compute the maxsizes as you loop through the data initially, putting it into the grid. I suspect this is what Excel does to get the speed it has when resizing (as each piece of data is moved into Excel's internal data store, size calculations are done). This would lengthen the initial load, but speed up resizing. In both these situations, you would replace that calls to ResizeToFit with a call to your own method that uses special knowledge of the data to avoid having to visit every cell. If you want to tie into Essential Grid's resizing architecture, then you would have to derive from GridTextBoxCellModel and GridTextBoxCellRenderer, and override GridTextBoxCellRenerer.OnQueryPrefferedClientSize and do something other than measuring the string to find its length. When Essential Grid needs the optimal size of a size, it will call this method. It is this method that calls the measurestring as its default implementation. If you are willing to cut some corners like base the sizing on the same font, then you can speed things up. For example this code in a button handler sized 1000x10 cells between 1-2 seconds.
private void button1_Click(object sender, System.EventArgs e)
{
	int ticks = Environment.TickCount;
	this.gridControl1.BeginUpdate();
	Graphics g = Graphics.FromHwnd(this.gridControl1.Handle);
	for(int j = 1; j 

Loader.
Live Chat Icon For mobile
Up arrow icon