WindowsForms - C # - ReportViewer - Compound object

2

Good afternoon,

I'm starting to develop reports using ReportViewer for Desktop applications using WindowsForms (C #).

I'm having problems with objects made up of others, for example:

class Produto
{
   public int Codigo { get; set; } 
   public string Nome { get; set; }
   public Grupo Grupo { get; set; }
}

class Grupo
{
   public int Codigo { get; set; } 
   public string Nome { get; set; }
}

My data source for the report is a List of Products, the List of the product object with their respective groups are filled, but I get #Error when trying to put the Group (which in a datagridview would be Gsis .Model.Group) or when I try an expression of type = Fields! Group.Value.Description

What I am saying may be better understood in the image below:

I'm using Visual Studio Community 2015 on Windows 10.

Thanks for the help,

Marcos Gerene

    
asked by anonymous 11.04.2016 / 23:05

1 answer

1

After a while I was able to do this with Report Viewer, the complete example follows.

The class Produto and Grupo (Category) must have the following attribute: [Serializable]

[Serializable]
public class Produto
{
    public int Codigo { get; set; }
    public string Nome { get; set; }
    public decimal Preco { get; set; }
    public virtual Categoria Categoria { get; set; }
}

[Serializable]
public class Categoria
{
    public int Codigo { get; set; }
    public string Nome { get; set; }
    public virtual ICollection<Produto> Produtos { get; set; }
}

Formulas in ReportViewer can be in two ways:

Formula to display only one record:

=First(Fields!Categoria.Value.Nome, "DataSetProdutos")

Formula to display multiple records in table object:

=Fields!Categoria.Value.Nome

To pass the data to the report:

 private void Form1_Load(object sender, EventArgs e)
 {
        var produtos = new List<Produto>
        {
            new Produto { Codigo = 1, Nome = "Produto 1" , Preco = 20, Categoria = new Categoria { Codigo = 1, Nome = "Categoria 1"} },
            new Produto { Codigo = 1, Nome = "Produto 2" , Preco = 30, Categoria = new Categoria { Codigo = 2, Nome = "Categoria 2"} }
        };

        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetProdutos", produtos));
        this.reportViewer1.RefreshReport();
  }

In the link I took by reference it is suggested to create a parameterless constructor, but it worked without the same normally.

Print showing what worked:

References

link link

    
25.11.2016 / 18:58