Showing posts with label Data Binding. Show all posts
Showing posts with label Data Binding. Show all posts

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????

Monday, 2 October 2006

Winforms DateTime Databinding

ScottWinforms and databinding has significantly improved in .NET 2.0 and a line of code such as - txtName.DataBindings.Add("Text", DataObject, "ClientName"); - just works with both data and nulls :)

However, the DateTimePicker and its ability to handle nulls is not quite a simple. To handle nulls with the DateTimePicker a few extra lines of code is needed.

The format property of the DateTimePicker control needs to be set. I have chosen Custom and as such the custom format is also set.
dtpAppointment.Format = DateTimePickerFormat.Custom;
dtpAppointment.CustomFormat = "dd/MMM/yyyy HH:mm";


This next line is really the most crucial. Notice the only difference with the simple single data binding line above is now there is a true paramater at the end. This is the formattingEnabled argument and without formattingEnabled set to true the handling of nulls just does not work!!!
dtpAppointment.DataBindings.Add("Value", DataObject, "AppointmentDate", true);

Happy binding :)