Object List (with some objects as attributes) for DataGridView - C # [duplicate]

0

How do I show the Street or Zip Code from the list below in the DataGridView.

People List ( to show in datagridview )

List<Pessoa> Pessoas;

Person Class

using System;

public class Pessoa
{
    public int id { get; set; }
    public string Nome { get; set; }
    public Endereco Endereco { get; set; }
}

Class Address

using System;

public class Endereco
{
    public int id { get; set; }
    public string Rua { get; set; }
    public string cep { get; set; }
}

My intention is to change the value that appears. The folder plus class is appearing: Model.Endereco

    
asked by anonymous 13.10.2016 / 20:43

1 answer

1

Use Linq to format the data, DataGridView does not accept clustered objects, or list of a main object, and even rewriting ToString() , which I see as the wrong technique, would not be able to bring the two fields that are essential to your query.

Make an expression with Linq like this:

GridView.DataSource = Pessoas.Select(x => new 
{
  x.id,
  x.Nome,
  Rua = x.Endereco.Rua,
  Cep = x.Endereco.Cep
})
.ToList();

Links:

13.10.2016 / 20:50