Problem
When you’re binding a control to a list of objects by adding a BindingList to a BindingSource, you get the following exception:
System.InvalidOperationException: Objects added to a BindingSource’s list must all be of the same type.
Here’s the code causing this:
BindingList<Movie> movies = new BindingList<Movie>()
{
new Movie()
{
Name = "Pulp Fiction",
MPAARating = MPAARating.R
},
new Movie()
{
Name = "The Dark Knight",
MPAARating = MPAARating.PG13
}
};
this.MovieCollectionBindingSource.Add(movies);
Code language: C# (cs)
Solution
Instead of adding the BindingList to the BindingSource with Add(), set BindingSource.DataSource to the BindingList. Here’s an example:
BindingList<Movie> movies = new BindingList<Movie>()
{
new Movie()
{
Name = "Pulp Fiction",
MPAARating = MPAARating.R
},
new Movie()
{
Name = "The Dark Knight",
MPAARating = MPAARating.PG13
}
};
this.MovieCollectionBindingSource.DataSource = movies;
Code language: C# (cs)