Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用BindingSource显示datagridview中对象的多个列?_C#_.net_Winforms - Fatal编程技术网

C# 如何使用BindingSource显示datagridview中对象的多个列?

C# 如何使用BindingSource显示datagridview中对象的多个列?,c#,.net,winforms,C#,.net,Winforms,我想在datagridview中显示一个对象。对象(运动)有名称、权重,然后是一些位置对象。每个位置都有一个名称和一个值。我希望能够为每个运动在DGV各自的列中显示名称、重量、location1.name、location1.value、location2.name、location2.value 我已经成功地覆盖了Location的toString方法,但是每个位置只给我1列,我真的希望能够有两列。有没有办法做到这一点?我目前正在使用这样的BindingSource BindingSource

我想在datagridview中显示一个对象。对象(运动)有名称、权重,然后是一些位置对象。每个位置都有一个名称和一个值。我希望能够为每个运动在DGV各自的列中显示名称、重量、location1.name、location1.value、location2.name、location2.value

我已经成功地覆盖了Location的toString方法,但是每个位置只给我1列,我真的希望能够有两列。有没有办法做到这一点?我目前正在使用这样的BindingSource

BindingSource movementSource = new BindingSource();

movementsSource.DataSource = aircraft.movements;

MovementDataGridView.DataSource = movementsSource;
如果有任何不同,则移动是一个
列表


谢谢

我要做的是创建一个包装类,如下所示:

class MovementView {
   private Movement movement;
   public MovementView(Movement m) { movement = m; }

   public string Name { get { return movement.Name; } }
   // etc..
   public string Location1Name { get { return movement.Locations[0].Name; } }
   // etc..
}
然后将网格绑定到
列表

movementsSource.DataSource = aircraft.movements
    .Select(t => new MovementView(t))
    .ToList();