Reputation: 16290
I have this model:
public class CustomerType
{
public int Id {get; set;}
public string Name {get; set;}
public string Description {get; set;}
}
Then, I have a binding source on a WinForms form, and its bound to existing object by doing:
var customerType = repository.Get(id);
bindingSource.DataSource = customerType;
I want to clear the datasource so I do:
bindingSource.DataSource = null;
But I get the following exception: Cannot bind to the property or column Name on the DataSource.
Setting the variable customerType
to null doesn't clear the datasource.
So what is the correct way to clear the datasource?
Upvotes: 1
Views: 6429
Reputation: 388
Firstly, null the data source:
this.bindingSource.DataSource = null;
Then clear the rows:
this.bindingSource.Rows.Clear();
Then set the data source to the new list:
this.bindingSource.DataSource = this.GetNewValues();
Upvotes: 0
Reputation: 460108
You could try
bindingSource.DataSource = typeof(CustomerType);
This is what visual studio assigns by default.
Upvotes: 2