Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS - Keep a table the same width when hiding columns dynamically?

Greetings.

I have a SSRS 2005 report that shows prices of things. For some customers, I hide a column from the table (with an expression on the Visibility - hidden property).

When I do this, my table shrinks. I've searched long and hard for a way to dynamically resize this table (or to do something at design time to make it stay the same width), but I'm stuck.

Answers that simply state 'You can't do this' won't help me. I've read that already: http://forums.asp.net/t/1354956.aspx

I'm hoping that some smart soul of the SO community has a workaround for me. Thanks!

like image 565
JoeB Avatar asked Sep 17 '09 17:09

JoeB


People also ask

How do you set column width dynamically in SSRS?

You need to assign a static value to the column width. But the width will keep the same as in design surface, height might be increase based on its content. If you want to change column width dynamically, you could try the workaround in this article: SSRS Column Width Auto Size.

How do I make all the columns the same width in SSRS?

To set the column to a specific width, click the column header and choose Format > Column > Width (or right-click the column header and choose Column Width from the context menu).

How do I fix column width in SSRS?

To change column widthHover over the column handle edge that you want to expand. A double-headed arrow appears. Click to grab the edge of the column and move it left or right to adjust the column width.

How do you hide a column based on an SSRS expression?

To hide static columns in a table, matrix, or list. In Design view, select the table, matrix, or list to display the row and column handles. Right-click the column handle, and then click Column Visibility.


1 Answers

The only way I know how to accomplish this, is by altering your RDLC file during runtime. Basically, you can load up your RLDC file into memory (its just an XML file), locate the XML node that contains the width of your table - then modify the setting in memory. Once you have done that, you can refresh your reportViewer control using the RDLC file that is loaded in memory.

And yes, I have already done this, and it does work.

The following code example is to alter the data of an RDLC file in memory, via its XMLpath.

  Private Sub ModifyRDLCInMemory()

    Dim xmlDoc As XmlDocument = New XmlDocument
    Dim asm As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
    'create in memory, a XML file from a embedded resource
    Dim xmlStream As Stream = asm.GetManifestResourceStream(ReportViewer1.LocalReport.ReportEmbeddedResource)

    Try
      'Load the RDLC file into a XML doc
      xmlDoc.Load(xmlStream)
    Catch e As Exception
      MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
    End Try

    'Create an XmlNamespaceManager to resolve the default namespace
    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
    nsmgr.AddNamespace("nm", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition")
    nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner")

    'Loop through each node in the XML file
    Dim node As XmlNode
    For Each node In xmlDoc.DocumentElement.SelectNodes(String.Format("//nm:{0}[@rd:LocID]", "Value"), nsmgr)  'XPath to LocID node.. You will want to change this to locate your Table Width node. You may need to read up on XMLPath
      Dim nodeValue As String = node.InnerText  'Gets current value of Node
      If (String.IsNullOrEmpty(nodeValue) Or Not nodeValue.StartsWith("=")) Then
        Try
          node.InnerText = YOURNEWVALUE

        Catch ex As Exception
          'handle error
        End Try
      End If
    Next

    ReportViewer1.LocalReport.ReportPath = String.Empty
    ReportViewer1.LocalReport.ReportEmbeddedResource = Nothing
    'Load the updated RDLC document into LocalReport object.
    Dim rdlcOutputStream As StringReader = New StringReader(xmlDoc.DocumentElement.OuterXml)
    Using rdlcOutputStream
      ReportViewer1.LocalReport.LoadReportDefinition(rdlcOutputStream)
    End Using

  End Sub
like image 159
jgallant Avatar answered Oct 05 '22 03:10

jgallant