Shohel
Shohel

Reputation: 3934

Dynamic Column width for a RDLC report

Lets say that I have 10 columns to view report and I want to hide 3 of these columns at runtime based on the value of parameter which the user would select. This can be easily done by setting the column visibility of each of these 3 columns based on the value of the aforesaid parameter. It's perfectly fine up till here.

The problem is when the report shows up (with 3 columns hidden) the remaining 7 columns take up the place of the hidden columns and as a result the overall width of the table reduces accordingly. I do not want this to happen. i.e. I want the table width to remain constant.

That is to say the remaining columns width should somehow be able to expand so that the original overall width of the table remains same.

Is this possible to achieve?

Upvotes: 13

Views: 18032

Answers (2)

Davey van Tilburg
Davey van Tilburg

Reputation: 718

I could not find a solution that is natively supported by Reports, nor did I find a solution online that was satisfying. So I wrote some code to manipulate the XML before using it in the reportview. It redistributes the width of hidden columns:

https://github.com/DaveyvanTilburg/RDLCDynamicColumns

Example piece of the xml being altered (can find the full example in github)

The before:

<TablixColumn>
    <Width>4.20cm</Width>
</TablixColumn>
<TablixColumn>
    <Width>12.4567cm</Width>
</TablixColumn>
<TablixColumn>
    <Width>10.50cm</Width>
</TablixColumn>

The after, where the 2nd column will be hidden, and its width is redistributed over the visible columns:

<TablixColumn>
    <Width>10.428350cm</Width>
</TablixColumn>
<TablixColumn>
    <Width>0cm</Width>
</TablixColumn>
<TablixColumn>
    <Width>16.728350cm</Width>
</TablixColumn>

Upvotes: 0

Ian Preston
Ian Preston

Reputation: 39566

Column width is not natively expression based, but you can achieve something like this. Whether it works for you I think will depend on your specific report layout and how the workaround affects any other elements.

Anyway, a simple example. I've created a report against a DataSet with three fields:

enter image description here

I've set val2 to have its visibility controlled by a boolean parameter, HideColumn. This works fine.

Note that there are actually five columns in the table. For val1 and val3 there are actually two columns, and I have merged the fields in the columns together.

The key here is that when HideColumn is set to true, we show the extra columns for val1 and val3, and when it's false we hide the columns - basically the opposite of the visibility for val2.

SSRS will adjust the width of the merged fields accordingly based on which columns are visible:

enter image description here

enter image description here

So in this case it's working as required. For your example you'll need to think about sizing and the required width of these extra columns, but the principle is the same.

This will only work for set columns, i.e. not a matrix, but hopefully will be sufficient for you.

Upvotes: 23

Related Questions