简体   繁体   English

在datagridview中显示对象列表

[英]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. 好的,现在,我想在我的datagrid视图中显示此列表。 But I have one problem. 但是我有一个问题。 My "Cliente" class has a "Provincia" object. 我的“ Cliente”类具有“ Provincia”对象。 I need show in the data gridview the "description" atribute in provincia class. 我需要在数据gridview中显示provincia类中的“描述”属性。

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 . 您可以使用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. 显然,您希望将哪些属性显示在代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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