0

Im using binding source, which is by setting the data binding properties of each text box to display data at textbox from database, Me.PaymentTableAdapter.Fill(Me.RestaurantDataSet.Payment)

Can i know how to format date and time, for example 04 Jan 2020 and 23:00 displayed at textbox? The default displaying style is 2020/01/04 and 23:00:00

I have searched through google but none of the solutions working

CW_2434
  • 156
  • 10
  • https://learn.microsoft.com/en-us/dotnet/framework/winforms/how-to-create-a-bound-control-and-format-the-displayed-data – Mary Jan 04 '20 at 16:52
  • Add handlers to the [Format](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.format) and [Parse](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.parse) events of the TextBox [Binding](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.binding) – Jimi Jan 04 '20 at 16:54
  • @Jimi, there's no need to handle events unless you need something out of the ordinary. If the data is type `Date` then you can simply set the `FormattingEnabled` property to `True` and then set the `FormatString` property appropriately. If the time data is type `TimeSpan` then the same thing may be possible, although I'm not 100% sure. – jmcilhinney Jan 05 '20 at 01:40
  • @jmcilhinney Sure, if you don't need to handle CultureInfo settings / Language specific formats / in/out data validations (or conversions as Unix timestamps, System.Management extended DateTime format etc.), a hard coded format is good enough. Formatting can be applied to data columns representing a DateTime, a TimeSpan, a DateTimeOffset etc. – Jimi Jan 05 '20 at 02:17

1 Answers1

2

I just tested the following code and it worked as expected:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table As New DataTable

    With table.Columns
        .Add("Date", GetType(Date))
        .Add("Time", GetType(TimeSpan))
    End With

    With table.Rows
        .Add(#1/1/2000#, TimeSpan.FromHours(5.0))
        .Add(#2/4/2008#, TimeSpan.FromHours(10.25))
        .Add(#3/8/2016#, TimeSpan.FromHours(15.5))
        .Add(#4/12/2024#, TimeSpan.FromHours(20.75))
    End With

    BindingSource1.DataSource = table

    UnformattedDateTextBox.DataBindings.Add("Text", BindingSource1, "Date")
    UnformattedTimeTextBox.DataBindings.Add("Text", BindingSource1, "Time")
    FormattedDateTextBox.DataBindings.Add("Text", BindingSource1, "Date", True, DataSourceUpdateMode.OnValidation, Nothing, "dd MMM yyyy")
    FormattedTimeTextBox.DataBindings.Add("Text", BindingSource1, "Time", True, DataSourceUpdateMode.OnValidation, Nothing, "hh\:mm")
End Sub

I used four TextBoxes - two without formatting and two with. As you can see, I provided only the basic information when binding the unformatted TextBoxes but used an overload of Add with more parameters in order to specify that formatting was enabled and what the format should be when binding the formatted TextBoxes. In order to provide a format string, you must also provide a data source update mode and a null value. In this case, I have specified the default values for each. If you don't want to have to specify those values then you could go back to the more basic overload and then set the other required properties after creation:

With FormattedDateTextBox.DataBindings.Add("Text", BindingSource1, "Date")
    .FormattingEnabled = True
    .FormatString = "dd MMM yyyy"
End With

With FormattedTimeTextBox.DataBindings.Add("Text", BindingSource1, "Time")
    .FormattingEnabled = True
    .FormatString = "hh\:mm"
End With

Note that, if your time data is type Date as well, the principle is still the same. You simply use a different format string:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table As New DataTable

    With table.Columns
        .Add("DateTime", GetType(Date))
    End With

    With table.Rows
        .Add(#1/1/2000 5:00:00#)
        .Add(#2/4/2008 10:15:00#)
        .Add(#3/8/2016 15:30:00#)
        .Add(#4/12/2024 20:45:00#)
    End With

    BindingSource1.DataSource = table

    UnformattedDateTextBox.DataBindings.Add("Text", BindingSource1, "DateTime")
    UnformattedTimeTextBox.DataBindings.Add("Text", BindingSource1, "DateTime")

    With FormattedDateTextBox.DataBindings.Add("Text", BindingSource1, "DateTime")
        .FormattingEnabled = True
        .FormatString = "dd MMM yyyy"
    End With

    With FormattedTimeTextBox.DataBindings.Add("Text", BindingSource1, "DateTime")
        .FormattingEnabled = True
        .FormatString = "HH:mm"
    End With
End Sub
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46