Display class relationship in Datagridview?

Asked

Viewed 60 times

1

I’m trying to display a class relationship in Datagridview but I’m not getting it. I have 3 classes Produto and Unidade and ItemVenda, the class Unidade refer to the unit of the product if it is KG/LT/UN etc... In Datagridview I want to display the Itemvenda that is the Product and its Unit but, I can only display the Product and when I try to display the Unit does not work. When I display the query result on a foreach the drive is displayed but I cannot display in Datagridview.

How to do this ?

I’m trying like this.

Classes

public class Unidade{
   public Integer id {set;get;}
   public String descricao {set;get;};

   public Unidade(){}
}

public class Produto{
    public Long id {set;get;};
    public String descricao {set;get;};
    public Unidade unidade {set;get;}

    public Produto(){}
}

public class ItemVenda{
    public Long id {set;get;}
    public Produto produto {set;get;}

    public ItemVenda(){}
}

Displaying on the Datagridview

private void defineGrid(){
   gridItensVenda.AutoGenerateColumns = false;
   IList<ItemVenda> lista = new ItemVendaDAO().findItensByVenda(venda);
   gridItensVenda.DataSource = lista;

   //exibe o produto
   DataGridViewColumn c1 = new DataGridViewTextBoxColumn();
   c1.DataPropertyName = "produto";
   c1.HeaderText = "Produto";

   //exibe a unidade do produto
   DataGridViewColumn c2 = new DataGridViewTextBoxColumn();
   c2.DataPropertyName = "ItemVenda.produto.unidade";
   c2.HeaderText = "Unidade";

   //adiciona columns ao grid
   gridItensVenda.Columns.Add(c1);
   gridItensVenda.Columns.Add(c2);   

}
  • This using Entity framework?

No answers

Browser other questions tagged

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