0

如何在 RDLC 报告中显示子属性值?例如:

public class Person
{
  public Address Address { get; set; }
}
public class Address 
{
  public string streetName {get;set;}
}

如何在 RDLC 报告中显示 streetName 值?

4

2 回答 2

1

正如您在Brian Hartman的这篇博客文章中看到的那样,自 VS2010 以来,LocalReport 中的嵌套类发生了变化。

他建议添加属性 [Serializable()](如果适用)。

您的课程将如下所示:

[Serializable()]
public class Person
{
  public Address Address { get; set; }
}
[Serializable()]
public class Address 
{
  public string streetName {get;set;}
}

编辑:在一些评论后添加了代码。尝试像这样实现你的类:

[Serializable()]
public class Person
{
    private Address _address;
    public Address Address
    {
        get
        {
            if(_address == null)
                return new Address(string.Empty);
            return _address;
        }
        set
        {
            _address = value;
        }
    }
    [Serializable()]
    public class Address
    {
        private string _streetName;
        public string streetName 
        { 
            get
            {
                return _streetName;
            }
            set
            {
                _streetName = value;
            }
        }
        public Address(string streetName)
        {
            _streetName = streetName;
        }
    }
}

这样,它将始终返回一个值,并且永远不会为空。

于 2013-01-18T15:34:04.997 回答
0

对于所有属性,我们必须在类中设置 [Serializable()]

[Serializable()]
public class Person
{
  public Address Address { get; set; }
}
[Serializable()]
public class Address 
{
  public string streetName {get;set;}
  public SubAddress SubAddress{get;set;}
}

[Serializable()]
public class SubAddress
{
  public string DoorNo {get;set;}
}

在报告属性的代码选项卡中的报告中使用以下代码

 Public Function GetName(ByRef obj As Object) As String 
     If obj Is Nothing Then Return "na" 
     Else : Return obj.streetName 
     End If 
 End Function 

并将报告字段获取为 =Code.GetName(Fields!Address.Value)

于 2013-01-22T13:18:50.893 回答