如何处理在RDLC报告无效嵌套对象绑定到自定义程序集对象的数据源?对象、嵌套、数据源、自定义

2023-09-03 07:32:48 作者:记忆的痕迹

我有一个RDLC报道,我直接渲染到响应流为PDF(而不是使用的ReportViewer)。在code呈现的报告,它的数据源绑定到一个列表(中ClassA的)的自定义组件定义的对象。这似乎工作的大部分。我的问题是,我似乎无法处理其中嵌套的对象是空的局面。例如,给定的ClassA和ClassB(嵌套物体)定义如下:

I have an RDLC report that I am rendering directly to the Response Stream as PDF (rather than using the ReportViewer). In the code that renders the report, it's DataSource is bound to a List(Of ClassA) objects defined in a custom assembly. This seems to work for the most part. My problem is that I can't seem to handle the situation where a nested object is null. For example, given ClassA and ClassB (the nested object) defined as follows:

    Public Class ClassA
       Public Id As Integer
       Public Name As String
       Public TheNestedObject As ClassB
    End Class

    Public Class ClassB
       Public Id As Integer
       Public Name As String
       Public TheParentObject As ClassA
    End Class

每当我试图有条件地显示一个N / A,如果B类是在我的前pression空如下:

Whenever I try to conditionally display an "N/A" if Class B is null in my expression as follows:

=IIf(IsNothing(Fields!TheNestedObject.Value,"n/a", Fields!TheNestedObject.Value.Name))

报告中显示#错误,如果TheNestedObject为空。如果TheNestedObject不为空,它正确显示名称。

the report displays "#Error" if TheNestedObject is null. If TheNestedObject is not null, it correctly displays the Name.

我在做什么错在这里?

谢谢!

推荐答案

在IIF函数计算所有的参数,从而场!TheNestedObject.Value.Name得到评估,并给出了错误,因为字段!TheNestedObject.Value为空。

The iif function evaluates all arguments and thus Fields!TheNestedObject.Value.Name gets evaluated and gives the error since Fields!TheNestedObject.Value is null.

我结束了加入一些自定义的code报告。这是在报告属性 - > code标签

I ended up adding some custom code to the report. It's under report properties -> Code tab.

Public Function GetName(ByRef obj As Object) As String
    If obj Is Nothing Then
        Return "n/a"
    Else : Return obj.Name
    End If
End Function

和那么你的文本框的前pression是:

And then your textbox expression is:

=Code.GetName(Fields!TheNestedObject.Value)

该函数返回N / A时,它的零和Name属性时,它不是。

The function returns "n/a" when it's null and the Name property when it's not.