Formatting Bound Data

There is one more topic I thought I'd cover, because it's a really cool feature, but also because it comes in quite handy when dealing with some types of data. If you remember back in the code where you set up the DataBindings for each control (Listing 5.6), the Add method returns the Binding class instance that it just created in the DataBindings collection. Normally, you can ignore this as shown in Listing 5.6 and move on; however, it's sometimes useful to hold onto that return value and add event handlers to a pair of useful events. the Format and Parse events of the Binding class work together to transform a value in the database to and from a different display format. The Format event is fired when the state of the binding changes and the binding manager is copying the current data out of the data source and into the UI, and then the Parse event fires when the binding manager is trying to take data from the UI and slam it back into the data source. One common use for the Format and Parse events is to transform a date time value stored in the database into the desired format and then to parse whatever information the user enters back into a datetime value before storing it into the data source. Listing 5.12 shows how you could use these two events to handle parsing a date.

Listing 5.12. Attaching to the Format Event
Private Sub Form1_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim conn As New SqlClient.SqlConnection( _
        "Integrated Security=SSPI;" & _
        "Initial Catalog=Northwind;Data Source=(local)")

    Dim cmdOrders As New SqlClient.SqlCommand( _
        "Select OrderID, OrderDate, RequiredDate" & _
        " From Orders", conn)
    Dim daOrders As New SqlClient.SqlDataAdapter(cmdOrders)
    daOrders.Fill(Me.dsNorthwind, "Orders")
    cmOrders = DirectCast( _
            Me.BindingContext( _
            dsNorthwind, "Orders"), _
        CurrencyManager)
    SetupControlBindings()
End Sub

Public Sub SetupControlBindings()
    Dim myBinding As Binding
    myBinding = Me.orderDate.DataBindings.Add( _
        "Text", dsNorthwind, "Orders.OrderDate")
    AddHandler myBinding.Format, AddressOf Binding_Format
    AddHandler myBinding.Parse, AddressOf Binding_Parse
End Sub.
				

As you can see, the new code takes the return value from the Add method and places it in a temporary variable to facilitate . Then it adds an event handler so when its Format Event is fired, the Binding_Format method will be called. Go ahead and write out that method using the code in Listing 5.13.

Listing 5.13. The Format and Parse Event Handlers
Private Sub Binding_Format(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ConvertEventArgs)
    Dim baseDate As DateTime
    baseDate = CDate(e.Value)
    e.Value = CStr(baseDate.ToShortDateString)

End Sub

Private Sub Binding_Parse(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ConvertEventArgs)
    Dim dateToSave As DateTime
    dateToSave = DateTime.Parse(e.Value)
    e.Value = dateToSave
End Sub

The “e” parameter has a DesiredType property so you can know what type the incoming binding wants the value to be, because it's defined in the data source of the binding manager. It also has a Value property, which is what the current value is at right now. The beauty of the Format Event is that you can set e.Value to whatever you want and that's what will be returned to the user interface to be displayed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.13.180