Search code examples
reportviewerrdlcline-breaks

RDLC doesn't break line when the word is too long


I have a RDLC Report with a Tablix. The Tablix has a Row bound to a DataSet. The Row has a Textbox with a width of 400px.

If you get a long string with spaces from the DataSet, it will behave as expected and break the string in new lines, making the textbox grow vertically. The problem happens when you get a long string without spaces from the DataSet, the string doesn't break when it reaches the end of the textbox. Instead, the textbox will grow horizontally to fit the string.

How can I break the string and prevent the Textbox from growing horizontally?


After consulting How to maintain long text inside RDLC report column ?

  • Counting the characters doesn't solve the problem: the data is coming from a database, so it can be virtually anything. And since I'm not using a console font, the size of the letters will not be the same, so the number of '@' that fit in a space is not the same number of 'i'.
  • CanGrow Property is bound to the TextBox: So even though you can select a column and set CanGrow to true, it will not set the Column itself to stop growing horizontally, it will just set all selected Textboxes 'CanGrow' property to False.
  • CanGrow Property only prevent the row from growing vertically: Even if you click on a Column. CanGrow Property only affects Height.

Solution

  • I've found the solution by myself. The RDLC report accepts HTML as Expression, so all you need to do is:

    • Set a div with a fixed width inside the expression.
    • Set the MarkupType as HTML

    See how the report code changes:

    Before:

    <Paragraphs>
        <Paragraph>
            <TextRuns>
                <TextRun>
                    <Value>=Fields!TextoCliente.Value</Value>
                    <Style>
                        <FontSize>8pt</FontSize>
                    </Style>
                </TextRun>
            </TextRuns>
            <Style />
        </Paragraph>
    </Paragraphs>
    

    After:

    <Paragraphs>
        <Paragraph>
            <TextRuns>
                <TextRun>
                    <Value>="&lt;div style='width:400px'&gt;" &amp; Fields!TextoCliente.Value &amp; "&lt;/div&gt;"</Value>
                    <MarkupType>HTML</MarkupType>
                    <Style>
                        <FontSize>8pt</FontSize>
                    </Style>
                </TextRun>
            </TextRuns>
            <Style />
        </Paragraph>
    </Paragraphs>
    

    You can set the Expression in the Designer viewer to avoid having to deal with the replacement of '<' with '&lt', in that mode, you can just type <.