Visual Studio 2012中的RDLC报告 - 对象实体

时间:2013-01-18 11:25:13

标签: entity-framework rdlc

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

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

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

2 个答案:

答案 0 :(得分:1)

正如您可以在Brian Hartman的this blog's post中读到的那样,自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;
        }
    }
}

这样它总会返回一个值,永远不会为空。

答案 1 :(得分:0)

对于所有属性,我们必须在Class

中设置[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)