I think the trick part of that is that you would need reflection to determine what you are dealing with Decimal, int, Datetime, etc
here's what you could do. Totally unproven
1) Create this class:
Public Class CustomBinding
Inherits System.Windows.Forms.Binding
Private mFormatString As String
Sub New(ByVal propertName As String, ByVal dataSource As Object, ByVal dataMember As String, ByVal formatString As String)
MyBase.New(propertName, dataSource, dataMember)
mFormatString = formatString
End Sub
Public ReadOnly Property FormatString()
Get
Return mFormatString
End Get
End Property
End Class
2) Here are the functions that wraps the databinding. I had them in my BaseForm
Protected Sub BindFieldCustom(ByVal control As Control, ByVal PropertyName As String, _
ByVal DataSource As Object, ByVal DataMember As String, ByVal formatString As String)
Dim bd As CustomBinding
Dim i As Integer
For i = control.DataBindings.Count - 1 To 0 Step -1
bd = control.DataBindings.Item(i)
If bd.PropertyName = PropertyName Then
control.DataBindings.Remove(bd)
End If
Next
bd = New CustomBinding(PropertyName, DataSource, DataMember, formatString)
control.DataBindings.Add(bd)
End Sub
Protected Sub BindFieldCustom(ByVal control As Control, ByVal PropertyName As String, _
ByVal DataSource As Object, ByVal DataMember As String, ByVal formatString As String, _
ByVal bdFormatDelate As ConvertEventHandler, _
ByVal bdParseDelate As ConvertEventHandler)
Dim bd As CustomBinding
Dim i As Integer
For i = control.DataBindings.Count - 1 To 0 Step -1
bd = control.DataBindings.Item(i)
If bd.PropertyName = PropertyName Then
control.DataBindings.Remove(bd)
End If
Next
bd = New CustomBinding(PropertyName, DataSource, DataMember, formatString)
AddHandler bd.Format, bdFormatDelate
AddHandler bd.Parse, bdParseDelate
control.DataBindings.Add(bd)
End Sub
Protected Sub BindField(ByVal control As Control, ByVal PropertyName As String, _
ByVal DataSource As Object, ByVal DataMember As String)
Dim bd As Binding
Dim i As Integer
For i = control.DataBindings.Count - 1 To 0 Step -1
bd = control.DataBindings.Item(i)
If bd.PropertyName = PropertyName Then
control.DataBindings.Remove(bd)
End If
Next
control.DataBindings.Add(PropertyName, DataSource, DataMember)
End Sub
Protected Sub BindField(ByVal control As Control, ByVal PropertyName As String, _
ByVal DataSource As Object, ByVal DataMember As String, _
ByVal bdFormatDelate As ConvertEventHandler, _
ByVal bdParseDelate As ConvertEventHandler)
Dim bd As Binding
Dim i As Integer
For i = control.DataBindings.Count - 1 To 0 Step -1
bd = control.DataBindings.Item(i)
If bd.PropertyName = PropertyName Then
control.DataBindings.Remove(bd)
End If
Next
bd = New Binding(PropertyName, DataSource, DataMember)
AddHandler bd.Format, bdFormatDelate
AddHandler bd.Parse, bdParseDelate
control.DataBindings.Add(bd)
End Sub
3) Here is the formatting and Parsing
Protected Sub FormatText(ByVal sender As Object, ByVal cevent As ConvertEventArgs)
Try
'THIS IS UNFINISHED
'YOU WILL NEED A CASE STATEMENT TO DETERMINE WHAT TYPE OF OBJECT YOU HAVE
'Here is an example data format
cevent.Value = CDate(cevent.Value).ToString(CType(sender, CustomBinding).FormatString())
Catch
cevent.Value = Date.Parse("1/1/1900").ToShortDateString
End Try
' DisplayBrokenRules()
End Sub
Protected Sub ParseText(ByVal sender As Object, ByVal cevent As ConvertEventArgs)
Try
'THIS IS UNFINISHED
'AGAIN You need a case statement to handle different types
'This only handles Dates
If CStr(cevent.Value) = "" Then
cevent.Value = "1/1/1900"
Else
cevent.Value = CDate(cevent.Value)
End If
Catch
cevent.Value = sd.DBValue
End Try
End Sub
4) Example Call
BindFieldCustom(txtOrderDate, "Text", mOrderData, "OrderDate", _
"d", _
New ConvertEventHandler(AddressOf FormatText), _
New ConvertEventHandler(AddressOf ParseText))