Windowsforms - C# - Reportviewer - Composite object

Asked

Viewed 838 times

2

Good afternoon,

I’m starting to develop reports using Reportviewer for Desktop applications using Windowsforms (C#).

I’m having problems with objects composed by 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 Product List, the Product Object List with their respective groups are filled in, but I get #Error when trying to place the Group (which in a datagridview would be Gsis.Model.Group) or when I try an expression like =Fields! Group.Value.Descricao

What I am saying can be better understood in the image below: Erro no relatório em ReportViewer

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

Grateful for the help,

Marcos Gerene

1 answer

1


After a while I managed to do this with Report Viewer, follow full example.

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 the 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 constructor without parameters, but it worked without the same normally.

Print showing that it worked:

inserir a descrição da imagem aqui

References

http://wraithnath.blogspot.com.br/2011/04/reportviewer-object-datasource-nested.html https://stackoverflow.com/questions/33577425/visual-studio-2013-rdlc-how-to-display-values-from-navigation-properties

Browser other questions tagged

You are not signed in. Login or sign up in order to post.