Monday 2 April 2007

Winforms datasource runtime change

ScottSimply put, i wanted to change the datasource of a binding source at runtime. The design behind this is a tab control where all tabs share a common control, therefore the common control needs to be context sensitive to the selected tab so as to display the correct data. Simple enough!!! So why does the code below through generic event handler exceptions???

_MyBindingSource.Clear() // Clears the binding source.
_MyBindingSource.DataSource = CurrentTabContextSensitiveData.
_MyBindingSource.RefreshBindings() // Common control to refresh the bound data.

OR

_MyBindingSource.DataSource = null;
_MyBindingSource.DataSource = CurrentTabContextSensitiveData.
_MyBindingSource.RefreshBindings() // Common control to refresh the bound data.

I dont know why these code samples cause generic exceptions!!

The code that does actually work is;
_MyBindingSource = new BindingSource();
_MyBindingSource.DataSource = CurrentTabContextSensitiveData.
_MyBindingSource.RefreshBindings() // Common control to refresh the bound data.

I hate it when a solutions is found but cant be fully explained or understood. Any ideas out there????