简体   繁体   English

DataGridView C#绑定多个列表

[英]DataGridView C# binding multiple Lists

Basically I have a List within a List that i want to bind to a dataGridView. 基本上,我有一个要绑定到dataGridView的列表中的列表。 It's like this 就像这样

public class Town {
    public List<Shop> Shops { get; set; }
}

public class Shop {
    public List<Car> Cars {get; set; }
}

and the class Cars with some properties and overriding the ToString method. 以及具有某些属性并覆盖ToString方法的Cars类。

Now, I want to display on the grid rows the Shops with the Cars's properties in the columns. 现在,我想在商店的网格行上显示列中具有汽车属性的商店。

I also serialize and deserialize the Town object into an XML. 我还将序列化Town对象并将其反序列化为XML。 I found a way to modify the DataSource object so it can store user's input, but I cannot display the data from a loaded XML. 我找到了一种修改DataSource对象的方法,以便它可以存储用户的输入,但是我无法显示来自已加载XML的数据。

It is not apparent what the problem is. 问题出在哪里还不清楚。 From what you've said, it should be fairly straightforward. 从您所说的来看,它应该相当简单。 Deserialize the Town object back into an object graph and you'd be able to do something like this: 将Town对象反序列化为对象图,您将可以执行以下操作:

var shop = town.Shops[0];
grid.DataSource = shop.Cars;

and that should be enough, provided your grid is correctly configured at design-time. 只要在设计时就正确配置了网格,就足够了。

The Car type must expose its data as public properties for this to work, not as fields. Car类型必须将其数据公开为公共属性才能正常工作,而不是作为字段公开。

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

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