简体   繁体   中英

DataGridView C# binding multiple Lists

Basically I have a List within a List that i want to bind to a 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.

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. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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