更新时间:2023-12-06 10:42:04
我知道如何完成此任务的唯一方法是在运行时更改 RDLC 文件.基本上,您可以将 RLDC 文件加载到内存中(它只是一个 XML 文件),找到包含表格宽度的 XML 节点 - 然后修改内存中的设置.完成此操作后,您可以使用加载到内存中的 RDLC 文件刷新您的 reportViewer 控件.
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.
以下代码示例是通过其 XMLpath 更改内存中 RDLC 文件的数据.
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