The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

Databinding to business objects and exceptions
One cool, and not quite new, feature of Visual Studio 2005 is data binding to your own business objects. Just create a new business object class, add a property, add it to the Data Sources window as an object data source and drag it onto your form. Simple as that and you are good to go.

However as usual there is a gotcha :-(
It throw an exception in a property get or get during the data binding the exception is eaten by the DataBindings object. There are two ways around this, either pass False as the formattingEnabled parameters or add an event handler to the BindingComplete event and check for the Exception property being set.

The first option seems the easiest but isn’t :-( Even if you set the Format type to no formatting in the Formatting and Advanced Binding dialog the formattingEnabled will still be true.

That leaves the second option. To handle the BindingComplete event you need to ass an event handler to all Binding objects on the form. Not difficult once you have a reference to all controls. The following code does just that.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each control As Control In GetAllControls(Me)
            For Each binding As Binding In control.DataBindings
                AddHandler binding.BindingComplete, AddressOf BindingCompleteHandler
            Next
        Next

    End Sub

    Private Sub BindingCompleteHandler(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)
        If e.Exception IsNot Nothing Then
            Throw New Exception("Unhandled exception during databinding actions.", e.Exception)
        End If
    End Sub


    Private Function GetAllControls(ByVal container As Control) As List(Of Control)
        Dim result As New List(Of Control)
        Dim pos As Integer = 0
        result.Add(container)
        Do While pos < result.Count
            For Each control As Control In result(pos).Controls
                result.Add(control)
            Next
            pos += 1
        Loop
        Return result
    End Function
End Class

Public Class SomeData

    Private _lastName As String = "Maurice de Beijer"
    Public Property LastName() As String
        Get
            Throw New Exception("Get: " + _lastName)
            Return _lastName

        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property
End Class


Maurice de Beijer
www.TheProblemSolver.nl


Published Wed, Jan 18 2006 13:09 by Maurice

Comments

# re: Databinding to business objects and exceptions@ Monday, October 09, 2006 1:42 PM

Why in @#ll would I resort to jumping thru  hoops in order to deal w/ .Net's bugs(err, xcuse,me , features), when i can do the same thing in a much more readable, smaller , maintainable manner?.  The goal of software engieering is to create solutions for real-world issues, not to massage one's ego by seeing demonstrating the latest and greatest features of yet another square wheel, with special interfaces  and abstract classes to deal with the occasionans of linear motion across flat surfaces.

# FREE SpiderWeb Marketing System - www.TheSpiderWebSystem.com@ Saturday, June 28, 2008 7:49 PM

Some people new to the Internet get confused by all the virtual terminology. This article on Internet Terminology should help.