1

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?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

2 Answers2

2

You could try

bindingSource.DataSource = typeof(CustomerType);

This is what visual studio assigns by default.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This does clear the bindingsource's datasource. But if I set the variable `customerType` to null, the datasource isn't cleared. Should it be cleared? – Ivan-Mark Debono Feb 26 '15 at 13:31
  • @Ivan-MarkDebono: i don't understand, does it work or not? I don't know why the `BindingSource` refuses to work if you assign `null`. I guess because you are using a custom type as source. The error message is rather clear. The BindingSource is looking for a `Name` property, `null` doesn't have this property, it belongs to the type `CustomerType`. – Tim Schmelter Feb 26 '15 at 13:49
  • I have found this related: http://stackoverflow.com/questions/220392/cannot-bind-to-the-property-or-column-name-on-the-datasource-parameter-name-da – Tim Schmelter Feb 26 '15 at 13:54
-1

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();
DeepakJ
  • 378
  • 4
  • 14