1

I'm making a program to save and show match statistics in Visual Basic .NET. Now I just have a problem with showing the date in my program. Reading from the database (MS Access) goes, and I show everything in a DataGridView.

For showing the date, I use the line dgvWedstrijden.Rows.Item(n).Cells(1).Value = dataReader(1).Date. But when I do this, the date shows as 9/10/2014 00:00:00. If I don't type the .Date, then the hour is not 00:00:00, but 14:15:26.

Instead of the line I showed before, I also used:

Dim datum As DateTime = FormatDateTime(dataReader(1), 1)
dgvWedstrijden.Rows.Item(n).Cells(1).Value = datum.Date

but this gives the same result.

Can someone help me how to show the date without the time (so without the 00:00:00)? I can't use ToString, because then it's not possible to order the column by date.

Thank you

Public Sub updateWedstrijden()
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim dataReader As OleDbDataReader
    Dim addnewcommand = New OleDb.OleDbCommand

    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = D:\De Koarters\Kalender\Minivoetbal\Minivoetbal\DatabaseMinivoetbal.accdb"
    con.ConnectionString = dbProvider & dbSource

    dgvWedstrijden.Rows.Clear()

    addnewcommand = New OleDb.OleDbCommand("SELECT SeizoenTornooi,Datum,Startuur,Tegenstander,DoelpuntenTegen,DoelpuntenVoor FROM Wedstrijden", con)
    con.Open()

    dataReader = addnewcommand.ExecuteReader
    While dataReader.Read()
        Dim n As Integer = dgvWedstrijden.Rows.Add()
        dgvWedstrijden.Rows.Item(n).Cells(0).Value = dataReader(0)
        dgvWedstrijden.Rows.Item(n).Cells(1).Value = dataReader(1).Date
        dgvWedstrijden.Rows.Item(n).Cells(2).Value = dataReader(2).ToString.Substring(0, 2) & "u" & dataReader(2).ToString.Substring(2, 2)
        dgvWedstrijden.Rows.Item(n).Cells(3).Value = dataReader(3)
        dgvWedstrijden.Rows.Item(n).Cells(4).Value = dataReader(4) & "-" & dataReader(5)
    End While

    dataReader.Close()
    con.Close()

    dgvWedstrijden.Sort(dgvWedstrijden.Columns(0), ListSortDirection.Ascending)
End Sub
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Glenn Decleir
  • 13
  • 1
  • 3

2 Answers2

3

It's just a problem of formatting, store in the cell the date as is so you won't loose time part in ordering and change column formatting to show only date part:

dgvWedstrijden.Columns(1).DefaultCellStyle.Format = "d"

While dataReader.Read()
    dgvWedstrijden.Rows.Item(n).Cells(1).Value = dataReader(1)
End While
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
0

Rather than manipulate the string on a cell-by-cell basis there, you could format the column on the dgv. Should let you retain type for potentially simpler sorting later on.

dgView.Columns["<Column Name Here>"].DefaultCellStyle.Format = "dd/MMM/yyyy"