如何处理绑定到自定义程序集对象数据源的 RDLC 报告中的空嵌套对象? [英] How do I handle null nested objects in RDLC report that is bound to custom assembly object datasource?

查看:30
本文介绍了如何处理绑定到自定义程序集对象数据源的 RDLC 报告中的空嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份 RDLC 报告,我将其作为 PDF 直接呈现给响应流(而不是使用 ReportViewer).在呈现报表的代码中,它的 DataSource 绑定到自定义程序集中定义的 List(Of 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"时,如果我的表达式中的 Class B 为空,如下所示:

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 为空,报告会显示#Error".如果 TheNestedObject 不为空,则正确显示名称.

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

我在这里做错了什么?

谢谢!!!

推荐答案

iif 函数评估所有参数,因此 Fields!TheNestedObject.Value.Name 被评估并给出错误,因为 Fields!TheNestedObject.Value 为 null.

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

我最终在报告中添加了一些自定义代码.它位于报告属性 -> 代码选项卡下.

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

然后你的文本框表达式是:

And then your textbox expression is:

=Code.GetName(Fields!TheNestedObject.Value)

该函数为 null 时返回n/a",非 null 时返回 Name 属性.

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

这篇关于如何处理绑定到自定义程序集对象数据源的 RDLC 报告中的空嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆