user2666350
user2666350

Reputation: 65

How to bind all the items in a list to DataGridView?

I have a class Zones which has a list of objects of type Row. What I'm trying to do is get a DataGridView to display all the Rows in all of my zones. This code only gives me the rows for the first zone in the list aircraft.zones.

BindingSource mySource = new BindingSource();  
mySource.DataSource = aircraft.zones;
mySource.DataMember = "rows";

The Zone class looks like this

public class Zone
{
    public List<Row> rows{ get; set; }
    public string name { get; set; }
    double arm;
    private int id;
}

I can do it by inserting all the rows into a DataTable then binding to that but I don't really want to do then any changes won't be updated. Any ideas how I can get all the rows in all the zones to show up?

Thanks.

Upvotes: 2

Views: 258

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103467

Try this, I think it will work including updates, although not inserts or deletes:

mySource.DataSource = aircraft.zones.SelectMany(t => t.rows).ToList();

(And remove the DataMember assignment)

Upvotes: 2

Related Questions