Get row height based on text length in a cell

Posted by: droopy5 on 8 September 2017, 1:42 pm EST

  • Posted 8 September 2017, 1:42 pm EST

    Hi...


       I have few columns in my spread where the 3rd row cells are TextCellType .


      It has Multiline set to True and WordWrap set to False. The Text Orientation : TextVerticalFlipped. The cell has text displayed in two lines.


     I want to resize the row height  based on the text present in the cell. For eg: if in a cell, the first line has 10 characters and second line has 15 characters,


      then the row height must expand based on the text with maximum length.(15 characters here). I used GetPreferredRowHeight() method but it returns the   rowheight for the two lines put together (so the row height is too big).How do i capture only the line with bigger text length within the same cell??..... Am using C#


     


    Thanks


    Droopy

  • Replied 8 September 2017, 1:42 pm EST

    hi...


    I have attached a screen shot of a part of my application.In the third row, i used GetPrefferedRowHeight() method to get the text length and increase the height of the row accordingly. But as you can see, it considers the length of  both the lines present in the same cell and not just one line. How do i set the height of cell to fit the contents perfect???(There musnt be so much of free space present).


     


    Thanks


    Droopy


    2009/01/sample.JPG
  • Replied 8 September 2017, 1:42 pm EST

    Hello,


    The GetPreferredRowHeight method should do this for you. You could use the GetPrefferedCellSize for each column in the row in question, but you should not have to do ths. Could you post a small zipped project reproducing this behavior for us to debug?

  • Replied 8 September 2017, 1:42 pm EST

    Droopy,


    I tested with the following code and it all worked correctly. Make sure you have the latest maintenance release. If you do, could you post the code you are using to test this?


    Dim t As New FarPoint.Win.Spread.CellType.TextCellType


    t.TextOrientation = FarPoint.Win.TextOrientation.TextVerticalFlipped


    t.Multiline = True


    FpSpread1.Sheets(0).Cells(1, 1).CellType = t


    FpSpread1.Sheets(0).Cells(1, 1).Value = "4098-9794EA" & Chr(10) & "4098-9717EA"


    FpSpread1.Sheets(0).Rows(1).Height = FpSpread1.Sheets(0).Rows(1).GetPreferredHeight

  • Replied 8 September 2017, 1:42 pm EST

    Hi..


       Only my main project has TextRenderer.dll.


        I tried the below code and it worked fine though i dint get the precise row height. Will this code work fine in all scenarios?Ill use this as a temporary fix and try the steps you suggested after this.


     


    Bitmap bitmap= new Bitmap(1, 1);


    Graphics graphics = Graphics.FromImage(bitmap);


    SizeF fontSize;


    fontSize = graphics.MeasureString(fpSpread.ActiveSheet.Cells[5, 6].Text.ToString(), new Font("Verdana", 10F));


    fpSpread.ActiveSheet.Rows[5].Height = (fontSize.Width);


     


    Thanks


     

  • Replied 8 September 2017, 1:42 pm EST

    Hi...


        I tried the GetPreferredHeight() method in a new project and it worked fine..In my main project i have many properties being set at the cell-level and row-level. I tried to find out the difference between my test and main project, but unable to figure out why the GetPreferredHeight() or GetPreferredRowHeight() methods do not work properly. In a textcelltype, with Multiline = true and text orientation as TextVerticalFlipped, if i have only one line of text, the resizing of row height works fine.But when i use a carriage return or newline constant to add text to the next line in the same cell, i get this problem.(i have attached the screen shots in my previous post)


    Is there any work around that could be done to get the exact preferred row height?...(As in get the height(or size) of the word with longer length and convert that value into float value to get the row height?)..Please help me out with this as this issue is being a show-stopper in my project.


    Thanks a lot!!


     

  • Replied 8 September 2017, 1:42 pm EST

    Hello,


    There needs to be a difference in how the Spread is setup in your project that is causing this issue. Is either of your projects referencing the TextRenderer.dll? In your sample project, did you flip the vertical orientation like your main project? Try using the Save method of Spread in your project to save the Spread settings and open the saved file in the Spread in a the new project and try your code to size the height. If this does not work, then you have a sample project you could send to us for debugging.

  • Replied 8 September 2017, 1:42 pm EST

    Hi scotts...


           Am using ' Spread for windows forms 4 (3.5 framework)' .


     I have used the same code as above.Instead of 'Value' property i gave the below code.


    FpSpread1.Sheets(0).Cells(1, 1).Text= "4098-9794EA" + "\n"+ "4098-9717EA";


    It dint work. It still gives that empty space as seen in my screen shot.I tried using 'Value' property too, but in vain.


    Is there any  work around that could be done to achieve this?..It is pretty urgent.


     


    Thanks


    Droopy

  • Replied 8 September 2017, 1:42 pm EST

    Hello,


    Make sure you are using the latest maintenance release for Spread for Windows Forms 4 (version 4.0.3509). If this is the release you are using, could you post a small zipped project reproducing the behavior for us to debug?

  • Replied 8 September 2017, 1:42 pm EST

    HI scotts....


         The version am using is 4.0.3503.....Does that mean i would not be able to get the desired row height in my application using the farpoint version that i already have?  Is there any other way to solve this issue with my current version?...I have to fix this asap as it is very important for my project. Could you please help me out


     Thanks

  • Replied 8 September 2017, 1:42 pm EST

    Hello,


    If you create a brand new project and use code like I posted and still see the issue, then there must be a bug in the release of Spread you are using. In this case, you would need to update to the latest free maintenance release on our web site. If it works in the new project, then we need to determine the difference between the Spread in your main project and test project.

  • Replied 8 September 2017, 1:42 pm EST

    Hello,


    The MeasureString method may not work in all situations. There are advantages and disadvantages of using GDI versu GDI+ painting. By default Spread uses GDI+ painting, but this is slower painting routines in the .NET framework. When you reference TextRenderer.DLL, Spread will paint using the old style GDI painting. This is quite a bit faster, but could have some issues like you are seeing. Unfortunately there is not a way, that I am aware of, to be 100% on painting controls in .NET. I would suggest using your code since you have it working. By the way, that should be similar code to what Spread would be doing by default if the TextRenderer.dll is not referenced (as you see in your sample project). Also, Spread has a CreateGraphics method you can use to get the Graphics object instead of having to create a temporary Bitmap.

Need extra support?

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

Learn More

Forum Channels