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