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

Automatically resize rows (height) to fit the heighest cell''s content

Is there a way (property) that I can automatically resize row height to fit the highest cell's content? When a user types something in a cell (cell accepts Returns) and the carriage return goes to the next line I would like to see the whole row's height adjust itself (grow or shrink).

10 Replies

CB Clay Burch Syncfusion Team September 17, 2009 11:18 PM UTC

There is no property setting to accomplish this task, but you can use an event to do it.

Here is a sample that uses the CurrentCellAcceptedChanges event to trigger setting the row height based on the edited cell contents. The event handler uses a Dispatcher.BegineInvoke to postpone the sizing code until after the CurrentCellAcceptedChnages code completes itself. If you do not do this, the cell style will not be updated with the new value yet.

A MeasureText method was included to determine the required row height. In the current code base, there is a defect in the grid.Model.ResizeRowHieghtsToFit method that would normally hanlde setting the height.



AutoSizeCells_c14ad5f1.zip


AS Andre Slenko September 21, 2009 08:52 PM UTC

In the sample application that you attached, the code doesn't work as expected because by the time BeginInvoke(...) fires, the CurrentCell is pointing to a new cell and it measures text for that new cell. How do I set my cells to accept returns and not to jump to a new cell (I want to type a large text with new line characters [Enter key] and see the row height adjusted as I type)? If I set the cell's value programatically (this.dgMain.Model[1, 1].CellValue = "Customer Safety"), what event can I catch to readjust the row height?


CB Clay Burch Syncfusion Team September 21, 2009 11:06 PM UTC

Here is the sample back tweaked some.

The cell being edited is saved in CurrentCellStartEditing.

Code has been added to take into account the margins (Border and Text) that may be set for the cell.

A method has been added that will autosize teh rowheights of a range of cells.

Note: All this will be simpler after the Vol4 release (mid Oct) that has built-in properly functioning methods, ResizeColumnWidthsToFit and ResizeRowHeightsToFit.



AutoSizeCells_d9b4bb20.zip


AS Andre Slenko September 22, 2009 02:57 PM UTC

Will those properties allow to type multi-line text? I would like to have an ability to accept returns and not to jump to a new cell (I would like to type a large text with new line characters [Enter key] and see the row height adjusted as I type). If I resize rows/columns, will those property adjust height/width (resizing column [width] will adjust applicable rows [height])?


CB Clay Burch Syncfusion Team September 22, 2009 06:44 PM UTC

Here is the sample tweaked to allow you to use the Enter key to add a return to the text in the cell.

I did not understand your last question.


AS Andre Slenko September 22, 2009 07:48 PM UTC

I was just running your sample and when I hit the Enter key the cell lost focus, I wasn't able to start the next line in the same cell. My last question is referring to the ability to resize row heights when column widths (containing long text) are changed by dragging column separators.
---------------------------------------------
COL1 COL2 COL3
---------------------------------------------
ROW1 Some very
long text
goes here
---------------------------------------------
ROW2 short text
---------------------------------------------
ROW3

If I expand COL1, I want ROW1 height to shrink accordingly.


CB Clay Burch Syncfusion Team September 23, 2009 11:59 AM UTC

Sorry, I did not upload the sample in my previous post. It is below. It should allow you to press enter in a cell and continue to type.

All the sizing code has been moved to a helper class for this sample.

With respect to autosizing the row heights as you change the column width, this can be problematic. Suppose the height of row 2 is determine by the text in cell (2,4). If you then size column 8, you would not want to size the rowheight of row 2 change because it is based on the text in cell (2, 4) and not (2, 8) (as that text is 'shorter' then the text in cell (2, 4). So, to avoid this issue, you would either have to always auto-size every cell in the grid as you are changing the column width, or you would have to maintain a collection of 'height-determining cells, one per row' to decide exactly when you want to adjust the row height. The first alternative would be slow for large, populated grids as computing optimal size is a performance hit. The second alternative would have the complexity of maintaining the special cells collection.





AutoSizeCells_fd309d4a.zip


CB Clay Burch Syncfusion Team September 23, 2009 01:02 PM UTC

Hiere is the sample modified to handle trying to autosize the row heights as you change teh column widths. If you run this sample, and immediately try to size column 2, you will see teh behavior you expect. If you then try to resize column 3, you will see that this resizing 'steps on' the sizing done when column 2 was sized.



AutoSizeCells_af8b852d.zip


JO Joseph Orjinta replied to Andre Slenko February 25, 2018 05:06 PM UTC

In the sample application that you attached, the code doesn't work as expected because by the time BeginInvoke(...) fires, the CurrentCell is pointing to a new cell and it measures text for that new cell. How do I set my cells to accept returns and not to jump to a new cell (I want to type a large text with new line characters [Enter key] and see the row height adjusted as I type)? If I set the cell's value programatically (this.dgMain.Model[1, 1].CellValue = "Customer Safety"), what event can I catch to readjust the row height?

WPF


TL Thirumurugan Loganathan Syncfusion Team February 26, 2018 06:36 PM UTC

Hi Joseph, 
 
If you set a cell value at programmatically in the grid control, QueryCellInfo event will invoke. Hence you can resize the row height at the event as in the below code sample. 
 
 
grid.Model.QueryCellInfo += Model_QueryCellInfo; 
 
private void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) 
{ 
            if (e.Style.RowIndex == 1 && e.Style.ColumnIndex == 1) 
                ResizeRowHeights(grid, GridRangeInfo.Cell(e.Style.RowIndex, e.Style.ColumnIndex)); 
} 
 
 
 
Regards, 
Thirumurugan 


Loader.
Live Chat Icon For mobile
Up arrow icon