0

I'm trying display relationship of Classes in DataGridView but I can't do it. I have 3 classes, Produto, Unidade and ItemVenda these classes contain relations between them and I can't display these relations in DataGridView. Using foreach in the Console displayed the relations fine.

How could I do it ?

Classes Relationship

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(){}
}

Trying display in DataGridView

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

   //Display Produto - works fine
   DataGridViewColumn c1 = new DataGridViewTextBoxColumn();
   c1.DataPropertyName = "produto";
   c1.HeaderText = "Produto";

   //Display Unidade of Produto doesn't work
   DataGridViewColumn c2 = new DataGridViewTextBoxColumn();
   c2.DataPropertyName = "produto.unidade";
   c2.HeaderText = "Unidade";

   //add columns to grid
   gridItensVenda.Columns.Add(c1);
   gridItensVenda.Columns.Add(c2);   

}
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • What does 'doesn't work' mean? No output? Exception? Wrong output?? Do not let us guess what your problems are! - Also:Can you show us how to result is supposed to __look__?? Finally: Can the class `Unidade` be displayed at all? It has no ToString method.. – TaW Apr 10 '16 at 15:24
  • You have some options to solve the problem: **•** You can override `ToString()` method of classes **•** You can add corresponding string property to your `ItemVenda` class **•** You can simply use `CellFormatting` event of grid to show the values in other columns of grid **•** You can shape the result using a linq query and bind the grid to a list of anonymous objects or a view model – Reza Aghaei Apr 10 '16 at 16:27
  • These posts may help you: •[How to bind a column from second level list on bindsource in winforms datagridview](http://stackoverflow.com/questions/36469904/how-to-bind-a-column-from-second-level-list-on-bindsource-in-winforms-datagridvi) or • [Show Properties of a Navigation Property in DataGridView (Second Level Properties)](http://stackoverflow.com/questions/35088181/show-properties-of-a-navigation-property-in-datagridview-second-level-propertie) – Reza Aghaei Apr 10 '16 at 21:08

1 Answers1

0

The DataPropertyName must refer to a property within the object displayed and I believe cannot navigate to sub-objects.

You can just add a property to the ItemVenda class to expose the unidade.

public class ItemVenda{
   // add this:
   public property Unidade unidade { get {return produto.unidade;} }
}

Then just use

DataGridViewColumn c2 = new DataGridViewTextBoxColumn();
c2.DataPropertyName = "unidade"; // not produdo.unidade
c2.HeaderText = "Unidade";
Stuart Whitehouse
  • 1,421
  • 18
  • 30
  • How could I use your suggestion with the NHibernate. I've this doubt because NHibernate use `virtual` on its properties and I don't want to add `Unidade` in database from `ItemVenda` because has `Unidade` on class `Produto`. – FernandoPaiva Apr 12 '16 at 14:25