Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS 2010 Report Viewer Cannot Access Nested Collections, Anyone?

I have gone through all the walk through on MSDN as usual they are worthless - extremely limited.

If I make the internal object in my class a single class I can display the information, but as soon as I convert it to a list of objects ( a collection ) I get the #Error in the display.

Here is an updated example.

For an example I have a Person object that can have one or more phone numbers ( list of numbers ) and I cannot find a way to access the phone numbers.

[Serializable]
public class Person
{
    private readonly List<PhoneNumber> _numbers = new List<PhoneNumber>();

    public Person()
    {
    }

    public Person(int id, string name, string address, decimal salary)
    {
        Id = id;
        Name = name;
        Address = address;
        Salary = salary;
    }

    public void AddNumber(PhoneNumber number)
    {
        _numbers.Add(number);            
    }


    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public List<PhoneNumber> PhoneNumbers { get { return _numbers; } }       

}

[Serializable]
public class PhoneNumber
{
    public PhoneNumber()
    {
    }

    public PhoneNumber(int id, string areaCode, string phone)
    {
        AreaCode = areaCode;
        Id = id;
        Phone = phone;
    }

    public string AreaCode { get; set; }
    public string Phone { get; set; }
    public int Id { get; set; }
}

I then populate the collections.

    var persons = new List<Person>();

    var t = new Person(1, "Mike", "5150 Nuts", 125);

    t.AddNumber(new PhoneNumber(1, "425", "455"));
    t.AddNumber(new PhoneNumber(1, "425", "450"));

    persons.Add(t);

    t = new Person(2, "Tom", "1055 MS HAS NO DOCUMENTATION AS USUAL!", 1245);

    t.AddNumber(new PhoneNumber(2, "TYPICAL", "OF-THEM"));
    t.AddNumber(new PhoneNumber(2, "ANY", "ONE???"));

    persons.Add(t);

I then assign everything to the report.

    reportViewer1.ProcessingMode = ProcessingMode.Local;
    reportViewer1.LocalReport.ReportPath = "..\\..\\Report1.rdlc";
    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Person",persons));
    reportViewer1.RefreshReport();

In the report it displays the people will display without issue as I add the text boxes to a list and then group the list by Id. When I try to display the phone numbers, I get the #ERROR message, and for the life of me I cannot seem to find a way to display the list of numbers that are assigned to a person.

If I change the object from List<PhoneNumber> within the person class to PhoneNumber I can access it, but when trying to display a List<PhoneNumber> I cant.

I need to be ale to display List<of objects> within an Class Item.

like image 247
nitefrog Avatar asked Nov 05 '22 19:11

nitefrog


1 Answers

The nested collection must be displayed as a subreport where the nested collection is an separate data source. You must bind the event LocalReport.SubreportProcessing to a handler that will filter and bind the datasource (PhoneNumbers) to the subreport as a seperate report data source. The example at the link provided should get you where you need to go.

like image 76
Cos Callis Avatar answered Nov 15 '22 04:11

Cos Callis