简体   繁体   中英

Show List of objects in datagridview

I have the following class:

class Cliente
{
    public int idCliente { get; set; }
    public String nombre { get; set; }
    public String apellidos { get; set; }
    public String dni { get; set; }
    public String telefono { get; set; }
    public String movil { get; set; }
    public String direccion { get; set; }
    public String localidad { get; set; }
    public Provincia provincia { get; set; }

    public Cliente()
    {
        this.provincia = new Provincia();
    }
}

I have a List of this class:

List<Cliente> listaClientes = new List<Cliente>();
listaClientes = gestorCliente.getClientesList();

Ok, and now, I want to show this list in my datagrid view. But I have one problem. My "Cliente" class has a "Provincia" object. I need show in the data gridview the "description" atribute in provincia class.

How can I do it?

  • Using dataset?
  • Using interface?

What is the better way to do it?

Thank you!!

You can bind it using LINQ select .

dataGridView1.DataSource = listaClientes.Select(c => new {c.idCliente, c.provincia.Description}).ToList();

Obviously put which attributes you wish to display into the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM