Friday, May 29, 2009

SSRS Column Width Auto Size

While fixing the problem described in my previous post I saw lots of people trying to set auto width for the columns. Without any luck though... I have found a way:

  1. Change Expression to be something like =”<DIV>” & Replace(Fields!YourField.Value," ","&nbsp;") & “</DIV>”
  2. Change placeholder properties – General tab – Markup type – HTML-Interpret HTML tags as styles. (Placeholder properties can be accessed by right clicking on Expression (Field) inside of the cell, not on cell itself).

That was too easy :)

PS. Works for SSRS 2008, never tested on 2005.

PPS. Keep in mind - this works ONLY in web browser. Export to PDF and even printing will still use predesigned column width. There might be a workaround too - but I don't really need it, so I will leave it to you guys.

Please comment if you find any additional notes.

32 comments:

  1. Where did you insert this expression? I tried horizontal alignment under "Texbox Properties" and under size in the Properties pane. Neither worked.

    fzlala@gmail.com

    ReplyDelete
  2. I changed the value field in placeholder properties and replaced it with the provided expression. I also did step 2. It didn't work :(

    ReplyDelete
  3. Right click on the Field, then select "Expression" from the popup menu.

    ReplyDelete
  4. Amazing! never thought of that. Thank you for this tip. Helped save a lot of time!

    ReplyDelete
  5. I did all this and tried different ways but it doesn't work. I use RS 2008. nay ideas why?
    tautvis@gmail.com

    ReplyDelete
  6. It was working with SSRS2008. Double check all the steps and play with columns properties. For the test - create project from scratch just to test this functionality.

    ReplyDelete
  7. Cant find step 2 in SSRS 2005

    ReplyDelete
  8. It was never tested in SSRS 2005 so please comment if you were able to make it work.

    ReplyDelete
  9. I followed the steps in SSRS 2008 R2 and it did not work while viewing the report in IE 8.

    ReplyDelete
  10. Works well for me - thanks dude, Now to work out a way (other than basing column widths on parameters) to make it work for pdf's :/

    ReplyDelete
  11. This will not work in ssrs 2005 as it does not support html formatting. ssrs 2008 does.

    ReplyDelete
  12. Aw, this was a really quality post. In theory I'd like to write like this too – taking time and real effort to make a good article… but what can I say… I procrastinate alot and never seem to get something done.

    ReplyDelete
  13. In my case the bold formatting was preserved in both screen view and export formats (tried pdf and excel only). :)

    ReplyDelete
  14. it works just fine...thanks alot.

    ReplyDelete
  15. Hello!

    I tried so many time, but I don't know why it is not working.
    I'm using SSRS 2008.

    ReplyDelete
  16. Hi. I want to set up my column width auto size. For exemple: In first page of my report I had in column1 same dates, and the second page my dates on column1 are larger then in first page. The column width size doesn´t grow up according the text size. Are you understanding me? I´m sorry about my english, it´s not perfect to explain things. Can somebody help meh? Thanks a lot

    ReplyDelete
  17. Thansk for your help, i was looking for this answer for a long time and found here.

    ReplyDelete
  18. Works great, but I lose my cell number formatting. Any thoughts how I can preserve that?

    ReplyDelete
  19. Got it to work with just text. How would I do this when my rows contain images

    ReplyDelete
  20. It did not work for me.. can you explain step by step to auto size width of column in SSRS

    ReplyDelete
  21. its not wrking for me..can u please say me the step

    ReplyDelete
  22. Excellent! Thank you very much.

    ReplyDelete
  23. It worked for me when I changed CanGrow set to "false" in the properties after following your instructions.
    Thank you.

    ReplyDelete
  24. 2008r2 Not working :(

    ReplyDelete
  25. Hi
    It didn't worked for me but if in the properties of the textbox you untick "sizing options" (in General area) this workaround works like a charm.

    ReplyDelete
    Replies
    1. There is one side effect with this solution when configuring via Report Builder alone so you may wish to down grade your charm setting and that is if the column width set at design-time is less than the expected contents width then the auto-fit solution works fine however, when exporting to PDF the auto-fit solution has no effect and thus the column becomes truncated because the sizing options (CanGrow=False) being turned off. On the other-hand if you set the width larger than the expected width the auto-fit does not work so it is not a full proof solution. So if your end user does require exporting to PDF then you are up the creek without a paddle and so you have to find the ideal width to set the column to that works both in preview and thus PDF layout and also browser layout. In my initial testing I had a further column namely a comments column that I left at the default setting, that is a fixed width and CanGrow=True and the comments were most of the time longer than the column that I wanted to auto-fit so in most cases exporting to PDF was ok, but as soon as I hid the comments column I noticed the flaw in the solution.

      Delete
  26. I tried this, no success. I am using expressions in an ssrs tablix. Now, the tablix header row just has straight text, no expression or field, as it contains the title of each column. Do I need to do anything special for that? jamarante@ciginsurance.com

    ReplyDelete
  27. I tried this and I initially had no success (using the instructions pointed out by lala's second comment above - i.e. you essentially click the cell twice so that the whole expression is highlighted then if you right click you should get from the popup menu either Create Placeholder or Place Holder Properties) however I realised the point from the author of the post above: "PPS. Keep in mind - this works ONLY in web browser" i.e. it does not work in Report Builder. After realising this I ran it and it did change the width however on my first page it was out by one character so I used for my expression =”<DIV>” & Replace(Fields!YourField.Value," "," ") & “ </DIV>”, i.e. a normal space before my closing div tag and seem to work. Fricken annoying!

    ReplyDelete
  28. Great post! Thanks! for the solution.

    Initially it doesn't worked for me but when I applied the solution to COLUMN HEADER of my matrix table, it worked!

    ReplyDelete
  29. Great post! Thanks! for the solution.

    Initially it doesn't worked for me but when I applied the solution to COLUMN HEADER of my matrix table, it worked!

    ReplyDelete
  30. Awesome, Thanks for solution.
    Worked for me!

    ReplyDelete
  31. This is working when i set CanGrow=False for the textbox. The text is not wrapped to second line when i export to PDF. What could be the solution that works in both pdf and web browser.

    ReplyDelete