显示DataGridView中类的成员

时间:2015-05-07 20:10:41

标签: c# winforms datagridview

我有两个班级:

public class Client
    {
     public int Id {get; set;}
     public string Name {get; set;}
     public AboutClient ClientInfo {get; set;}

     public Client (int id, string name, AboutClient aboutClient)
     {
      Id = id;
      Name = name;
      ClientInfo = aboutClient;
     }
    }

 public class AboutClient
    {
     public int Id {get; set;}
     public string LastName {get; set;}

     public AboutClient(int id, string lastName)
     {
      Id = id;
      LastName = lastName;
     }
    }

我创建了一个列表并将其绑定到datagridview

    public void BindData()
    {
     List<Client> clients = new List<Client>();

     clients.Add(new Client(1, "Joy", new AboutClient(1, "Smith")))

     dataGridViewClient.DataSource = clients;
    }

我在dgw中获得以下数据:

1       "Joy"      AboutClient

如何显示AboutClient.LastName而不是“AboutClient”?

2 个答案:

答案 0 :(得分:1)

最简单的方法是隐藏ClientInfo列:

[Browsable(false)]
public AboutClient ClientInfo {get; set;}

添加一个新列,返回客户端信息中的姓氏:

public string LastName { get { return ClientInfo.LastName; } }

答案 1 :(得分:0)

将此添加到AboutClient

public override string ToString()
{
    return LastName;
}