Ivan-Mark Debono
Ivan-Mark Debono

Reputation: 16290

How to clear a binding source?

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

Answers (2)

DeepakJ
DeepakJ

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

Tim Schmelter
Tim Schmelter

Reputation: 460108

You could try

bindingSource.DataSource = typeof(CustomerType);

This is what visual studio assigns by default.

Upvotes: 2

Related Questions