vb.netvisual-programming

Im using binding source to display data at textbox from database, can i know how to format date and time displayed at textbox?


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


Solution

  • 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