Using Row.GetPreferredHeight() returns invalid row heights.

Posted by: krishna.vajja on 28 May 2020, 6:47 pm EST

  • Posted 28 May 2020, 6:47 pm EST

    Hi,

    We are facing an issue with GetPreferredHeight().
    If we merge more than two rows and columns and both multiline and wordwrap are set to true
    And then try to get the preferred height of the row based on the text given,
    we are getting the output height more than the size of the text.
    But if we use the method for single row and multiple columns merge and both wordwrap and multiline are set to true,
    the method is giving us the correct size of the row based on the text given.

    Please help us asap in resolving this issue.

    Thanks,
    Krishna.
  • Replied 28 May 2020, 6:48 pm EST

    Untitled1.zipUntitled2.zip
  • Replied 29 May 2020, 12:03 am EST

    Hello,

    I have escalated the issue to the development team(SPNET-7694) and will inform you once I get any information from them.

    Thanks,
    Mohit
  • Marked as Answer

    Replied 1 June 2020, 6:53 pm EST

    Hello,

    As per the developer, it is by design. PreferredHieght doesn't work on the merge cell.

    Thanks,
    Mohit
  • Replied 20 January 2021, 9:42 pm EST

    Hi Mohit,

    We got a requirement for applying wordwrap on a merged cell(across multiple rows and columns).
    Do you have any future plans for that?
    Please let us know.

    Thanks,
    Krishna
  • Replied 20 January 2021, 10:43 pm EST

    Hi Mohit,

    In the earlier reply you said that WrapText doesn't work for merged cells. But, when I verified it in Spread Designer v12, wrap text was working fine when a cell is merged across multiple columns and rows.
    Can you let us know the details of methods for applying wordwrap for merge cells in the code.

    Thanks,
    Krishna
  • Replied 21 January 2021, 6:37 pm EST

    Hi Krishna,

    In the earlier post, Mohit meant that the GetPreferredHeight method does not work (correctly) when there are Merged cells since merged cells are ignored while calculating preferred height/width.

    You can apply WordWrap using:
    fpSpread1.ActiveSheet.Cells[1, 1].ColumnSpan = 2;
    fpSpread1.ActiveSheet.Cells[1, 1].RowSpan = 2;

    var cellType = new GeneralCellType();
    cellType.WordWrap = true;
    fpSpread1.ActiveSheet.Cells[1, 1].CellType = cellType;


    Regards,
    Jitender
  • Replied 21 January 2021, 8:16 pm EST

    Hi Jitender,

    Thanks for the solution.
    We were using wordwrap property since long time and the property has driven by GetPreferredRowHeight() method. This method is doing the job for us except while multiple rows are merged.
    I understand you have given us a new option now, but both the properties have slight differences. If we start using the new option, our clients who have to upgrade to new version will face issues and would not be ready to that.
    Hence, I request you to provide a fix for for the issues we are facing while using GetPreferredRowHeight(). I think you will also understand how incorrectly the method works when multiple rows are merged and the method is applied on it.

    I am also attaching the screenshot of the issue we are facing.
  • Replied 28 January 2021, 4:28 am EST

    Hi,
    Awaiting a response for the above query.

    Regards,
    Krishna.
  • Replied 28 January 2021, 10:48 pm EST

    Hi,

    The behavior of GetPreferredRowHeight is consistent with the way Excel works. In Excel also, if you merge multiple cells with WordWrap then the row height is not increased to accommodate the content because the merged cells are not considered in the calculation.

    Please refer to the attached sample which uses the cell Renderer to set the row height so that the content is still visible with WordWrap.

    MergedRowPrefHeight.zip

    Regards,
    Jitender
  • Replied 27 April 2021, 12:29 am EST

    Damm even I am having a similar kind of issue, I have searched all over the internet and even have posted on number of threads on different forum, no solution seems to work. I am really frustrated, can anyone of you here help me resolve this issue, I am very much tired now.

  • Replied 4 April 2021, 7:51 pm EST

    Hi,

    Please refer the sample attached in the previous post:
    https://www.grapecity.com/forums/c1-studio/using-rowgetpreferredheigh#hithe-behavior-of-getprefe

    The sample demonstrates how you can correctly calculate the preferred row heights when multiple rows are merged.
    Let us know if you face any issues in using the sample.

    Regards,
    Jitender
  • Replied 2 May 2021, 1:34 am EST

    Hi,
    I have executed the sample solution given. Thanks for that, but it is not serving the purpose.
    In the solution, the cell which is merged across multiple rows, input data which is more than 4 lines and apply wordwrap to it then, the first row among the merged rows size increases which may again affect the other cells in the same row.

    Attaching the screenshot
  • Replied 2 May 2021, 6:24 pm EST

    Hi,

    Can you please explain how exactly you want to use GetPreferredRowHeight?

    From your original post:
    krishna.vajja said:

    If we merge more than two rows and columns and both multiline and wordwrap are set to true
    And then try to get the preferred height of the row based on the text given,
    we are getting the output height more than the size of the text.
    But if we use the method for single row and multiple columns merge and both wordwrap and multiline are set to true,
    the method is giving us the correct size of the row based on the text given.


    I thought that you want to show the entire text content when the cell is wrapped by taking height from GetPreferredRowHeight and expanding the row height (if necessary).


    From your latest post:
    krishna.vajja said:

    In the solution, the cell which is merged across multiple rows, input data which is more than 4 lines and apply wordwrap to it then, the first row among the merged rows size increases which may again affect the other cells in the same row.


    It seems you don't want to change the merged rows height.


    Is it that you want to expand the row height when the merged cells have one row but not when multiple rows are merged?

    Regards,
    Jitender
  • Replied 9 May 2021, 10:23 pm EST

    Hi,

    I was little uncertain of it earlier. But after reviewing it with my team, we figured out that the output that is getting displayed in sample like expanding the row size to fit the data is acceptable in case of both single row and multiple rows.

    But, on trying it different values, we observed an issue i.e., sometimes the value is not completely displayed in the merged rows where wordwrap is set to true.

    The data which we used is : "It is similar to ref keyword. But the main difference between ref and out keyword is that ref needs that the variable must be initialized before it passed to the method."
    I am sharing the ssc also for reference.
    Let me know if you need more info or we missing anything.

    Thanks for the support.

  • Replied 11 May 2021, 9:52 pm EST

    Hi,

    The difference is because in the sample the grid line widths were not considered while calculating preferred row height.
    The attached sample shows how this can be used in the row height calculation.
    MergedRowPrefHeight_Mod.zip

    Let us know if you observe any issues with the sample.

    Regards,
    Jitender
  • Replied 12 May 2021, 8:19 pm EST

    Hi,
    The latest solution given to us is matching our requirement. But we have couple of things which are concerning us.
    • The given approach contains a lot of calculation for multiple rows and we think there would be a performance issue for us as there would be many such kind of cells where wordwrap applied and computing that method for multiple times may cause performance.
    • Will the objects like StyleInfo, Renderer and Graphics would reduce performance while we loop that method for hundreds of cells?
    • Can you please let us know if there is any other optimized solution for this and could you provide an api for us to use which contains all this computation rather than we make all computations in our codebase

    And we have a general question:
    • There are multiple ways of implementing wordwrap and we want to know like what your idea/approach was for having an implementation of wordwrap in such a way that it expands the row

    Thanks,
    Sreepriya.
  • Replied 12 May 2021, 11:25 pm EST

    Hi Sreepriya,

      The approach used is actually an approximation of what GetPreferredRowHeight does for its implementation. Unless you call this method for thousands of cells, there should be no issue. If that's the case, you can customize it to defer calculations for cells which are not visible.

      The StyleInfo, Renderer, Graphics object won't cause any issues as to get these objects, more performant APIs of Spread are used. If you profile the sample, you can see there is no memory issue. Besides, the suggested calculation uses these objects just like GetPreferredRowHeight uses them.

      The AutoFit API is meant for this purpose. However, it ignores merged rows to maintain compatibility with Excel. For example, if you click the bottom of the row for merged rows, AutoFit does not change the row heights.


      The particular approach presented here was simply to take the height calculation logic from GetPreferredRowHeight, modify it to make it work for merged rows. The approach used for merged rows is:

    1. Calculate the size of the merged cell (by considering column widths, row heights, and grid line width)
    2. Use cell Renderer's GetPreferredSize method to get the size required by text. Take Height from this.
    3. Reduce this Height by the heights for all except the first row of merged rows
    4. Add grid line height to this Height.
    5. Set the height of the first row (in merged rows) to the calculated Height.

    Hope that helps.

    Regards,
    Jitender
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels