|
-
Mar 19th, 2009, 09:57 PM
#1
Thread Starter
Hyperactive Member
event for when data is loaded from database
hi all,
i have a textbox that i want to format the value and i placed it in the Validated event.. it works ok when a user enters a value but when data is being loaded from database, it doesn't format.
how do i do this?
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Mar 19th, 2009, 10:20 PM
#2
Re: event for when data is loaded from database
The Validated event is only raised when the user tries to move focus away from a control, or else you explicitly call Validate or ValidateChildren in the form. Obviously none of those things are happening when you bind data.
That said, you shouldn't be formatting the data yourself if the control is bound. If you do then you're going to actually change the value stored in the data source. You should be letting the Binding handle that for you. Read the documentation for the Binding class and you'll see that there are several related to formatting. That way the value exposed by the Binding and displayed in the control can be different to the value in the data source.
-
Mar 20th, 2009, 12:36 AM
#3
Thread Starter
Hyperactive Member
Re: event for when data is loaded from database
hi,
i'm having a bit of a problem with this... i tried following the sample here http://msdn.microsoft.com/en-us/libr...ng.format.aspx but i noticed that i needed to hard-code the datasource when creating a new binding.
this is actually a user control that i'm creating. i'ts like a textbox but with a property Format that i created.. then when the data is loaded in the textbox, it should be formatted based on the format mask in the Format property.
now, the problem i have is that when
Code:
Dim b As Binding = New Binding ("Text", ds, "customers.custToOrders.OrderAmount")
'The "ds" as well as the "customers.custToOrders.OrderAmount" are not yet specified. and the databinding is not yet added initially.. this is only added when i set the user controls databinding property at design time.
what approach should i take?
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Mar 20th, 2009, 12:51 AM
#4
Re: event for when data is loaded from database
The Binding constructor is overloaded so you can specify the format when it's created, whether in code or in the designer. You can also set the format string in code at any time, e.g. when the Format property of your control changes.
-
Mar 20th, 2009, 01:18 AM
#5
Thread Starter
Hyperactive Member
Re: event for when data is loaded from database
 Originally Posted by jmcilhinney
The Binding constructor is overloaded so you can specify the format when it's created, whether in code or in the designer. You can also set the format string in code at any time, e.g. when the Format property of your control changes.
hi,
i somewhat get your point by i have little idea on how to implement it.
can you provide any sample?
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Mar 20th, 2009, 01:32 AM
#6
Re: event for when data is loaded from database
In post #3 you have shown the creation of a Binding. Can you change that code to include the setting of the format string? Have you read the Binding constructor documentation?
-
Mar 20th, 2009, 02:04 AM
#7
Thread Starter
Hyperactive Member
Re: event for when data is loaded from database
hi,
ok so i now have this
Code:
Dim ds As New DataTable
Dim b As Binding = New Binding("Text", ds, "", True, DataSourceUpdateMode.OnPropertyChanged, DBNull.Value, DateTimeFormatMask)
Me.DataBindings.Add(b)
at first i assumed that this binding will overwrite any databinding that has the same property but i get an error while doing so.
"This causes two bindings in the collection to bind to the same property.
Parameter name: binding"
so now i'm thinking this is not what i'm suppose to be doing.
any more tips?
thanks.
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Mar 20th, 2009, 04:38 AM
#8
Re: event for when data is loaded from database
There aren't any collections that I'm aware of where calling Add twice will overwrite anything. If you have already added a Binding then you can't add another one for the same property. You have to set the format string when you create the one and only Binding for that property and/or you need to get a reference to that Binding and set the appropriate property. Do you know the format string when you create the Binding in the first place?
-
Mar 20th, 2009, 06:05 AM
#9
Thread Starter
Hyperactive Member
Re: event for when data is loaded from database
 Originally Posted by jmcilhinney
Do you know the format string when you create the Binding in the first place?
not really. can you show me some sample syntax?
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Mar 20th, 2009, 11:19 AM
#10
Re: event for when data is loaded from database
If you don't know the format string when you create the Binding then you can't set it when you create the Binding. You have to set it later. When you DO know the format string you simply get the desired Binding from the DataBindings collection and then set the appropriate property.
-
Mar 20th, 2009, 05:55 PM
#11
Thread Starter
Hyperactive Member
Re: event for when data is loaded from database
hi,
the format string is set when provided at design time. i added a property for it. i can probably set a default format string.
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Mar 20th, 2009, 09:22 PM
#12
Re: event for when data is loaded from database
If the format string is provided at design time then you DO know the format string when the Binding is created, so you can set the format string for the Binding in the designer. Select DataBindings -> Advanced in the Properties window.
-
Mar 21st, 2009, 05:50 AM
#13
Thread Starter
Hyperactive Member
Re: event for when data is loaded from database
 Originally Posted by jmcilhinney
If the format string is provided at design time then you DO know the format string when the Binding is created, so you can set the format string for the Binding in the designer. Select DataBindings -> Advanced in the Properties window.
hi,
i'm lost...
here's all that i have for my usercontrol
Code:
Public Class DateTimeTextBox
Inherits TextBox
Public _Text As String
Private _DateTimeFormatMask As String = "MM/dd/yyyy"
Public Property DateTimeFormatMask() As String
Get
Return _DateTimeFormatMask
End Get
Set(ByVal value As String)
_DateTimeFormatMask = value
End Set
End Property
Private Sub DateTimeTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
Dim frm As New CalendarForm
Try
If Me.Text <> "" Then
frm.FormatMask = DateTimeFormatMask
Me.Text = UCase(Format(DateValue(Me.Text), DateTimeFormatMask))
End If
Catch ex As Exception
Me.Text = ""
End Try
End Sub
End Class
when i reference this control into one of my forms and bind its Text property to a column in a datatable, when data is loaded and shown in the control, i want it in the format of my DateTimeFormatMask.
that part is not working and i don't know how to make it work.
thanks
Last edited by adshocker; Mar 21st, 2009 at 07:37 AM.
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|