-1

I'm building an attendance management project in vb.net. It will filter data of students between two dates and show the attendance. I'm using ms access 2007 database. When I display date from my database to datagridview column it is showing date only which is what I want but when I'm printing the gridview it shows date along with 00:00:00 which is not right. Please help me with this. My professor wants it and he himself doesnt know how to solve.

I'm adding complete code here:

Imports System.Data.OleDb
Public Class completeattendace
    Dim con As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\User\AMS\Attendance.accdb")
    Dim pageCounter As Integer
    Dim startRow As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        con.Open()
        If ComboBox1.SelectedItem = "1BCASE01" Then
            Dim cmd As New OleDbCommand("Select stud_roll, stud_name, course_code, attend_date, status From Course1 Where attend_date between #" & DateTimePicker1.Value.Date & "# and #" & DateTimePicker2.Value.Date & "# order by attend_date asc", con)
            Dim da As New OleDbDataAdapter
            da.SelectCommand = cmd
            Dim dt As New DataTable
            dt.Clear()
            da.Fill(dt)
            DataGridView1.DataSource = dt
            con.Close()
        End If
        If ComboBox1.SelectedItem = "SKNKJAD" Then
            Dim cmd As New OleDbCommand("Select stud_roll, stud_name, course_code, attend_date, status From Course2 Where attend_date between #" & DateTimePicker1.Value.Date & "# and #" & DateTimePicker2.Value.Date & "# order by attend_date asc", con)
            Dim da As New OleDbDataAdapter
            da.SelectCommand = cmd
            Dim dt As New DataTable
            dt.Clear()
            da.Fill(dt)
            DataGridView1.DataSource = dt
            con.Close()
        End If
        If ComboBox1.SelectedItem = "MSNDAD" Then
            Dim cmd As New OleDbCommand("Select stud_roll, stud_name, course_code, attend_date, status From Course3 Where attend_date between #" & DateTimePicker1.Value.Date & "# and #" & DateTimePicker2.Value.Date & "# order by attend_date asc", con)
            Dim da As New OleDbDataAdapter
            da.SelectCommand = cmd
            Dim dt As New DataTable
            dt.Clear()
            da.Fill(dt)
            DataGridView1.DataSource = dt
            con.Close()
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        pageCounter = 1
        startRow = 0
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim actualWidth As Integer = DataGridView1.Columns.Cast(Of DataGridViewColumn).Sum(Function(c) c.Width)
        Dim percentage As Decimal = ((100 / actualWidth) * e.MarginBounds.Width) / 100
        Dim college As String = "College"
        Dim header As String = "Attendance Report"
        Dim course As String = "Course Code:-" + ComboBox1.SelectedItem
        Dim dateattend As String = "Date :-" + DateTimePicker1.Value.Date + "  -  " + DateTimePicker2.Value.Date
        Dim footer As String
        Dim startX As Integer = e.MarginBounds.Left
        Dim startY As Integer = e.MarginBounds.Top
        Dim r As Rectangle
        Dim collegeFont As New Font(DataGridView1.Font.FontFamily, 35, FontStyle.Bold, GraphicsUnit.Pixel)
        Dim szf As SizeF = e.Graphics.MeasureString(college, collegeFont)
        e.Graphics.DrawString(college, collegeFont, Brushes.Red, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, startY - szf.Height - 40)
        Dim headerFont As New Font(DataGridView1.Font.FontFamily, 25, FontStyle.Bold, GraphicsUnit.Pixel)
        szf = e.Graphics.MeasureString(header, headerFont)
        e.Graphics.DrawString(header, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, startY - szf.Height)
        Dim courseFont As New Font(DataGridView1.Font.FontFamily, 15, FontStyle.Bold, GraphicsUnit.Pixel)
        szf = e.Graphics.MeasureString(course, courseFont)
        e.Graphics.DrawString(course, courseFont, Brushes.Black, e.MarginBounds.Left, startY - szf.Height + 40)
        Dim datefont As New Font(DataGridView1.Font.FontFamily, 15, FontStyle.Bold, GraphicsUnit.Pixel)
        szf = e.Graphics.MeasureString(dateattend, datefont)
        e.Graphics.DrawString(dateattend, datefont, Brushes.Black, e.MarginBounds.Left, startY - szf.Height + 70)
        footer = "Page " & pageCounter.ToString
        Dim footerFont As New Font(DataGridView1.Font.FontFamily, 10, FontStyle.Regular, GraphicsUnit.Pixel)
        szf = e.Graphics.MeasureString(footer, footerFont)
        e.Graphics.DrawString(footer, footerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, e.MarginBounds.Bottom + 5)

        startY += 100

        'this is the text alignment
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center

        Dim gridFont As New Font(DataGridView1.Font.FontFamily, 12 * percentage, FontStyle.Regular, GraphicsUnit.Pixel)

        If startRow = 0 Then
            For x As Integer = 0 To DataGridView1.Columns.Count - 1
                r.X = startX
                r.Y = startY
                r.Width = DataGridView1.Columns(x).Width * percentage
                r.Height = DataGridView1.Rows(0).Height
                e.Graphics.DrawRectangle(Pens.Black, r)
                e.Graphics.DrawString(DataGridView1.Columns(x).HeaderText, gridFont, Brushes.Black, r, sf)
                startX += r.Width
            Next

            startY += r.Height
        End If

        For y As Integer = startRow To DataGridView1.Rows.Count - 1
            If y = DataGridView1.NewRowIndex Then Continue For
            startX = e.MarginBounds.Left
            For x As Integer = 0 To DataGridView1.Columns.Count - 1
                r.X = startX
                r.Y = startY
                r.Width = DataGridView1.Columns(x).Width * percentage
                r.Height = DataGridView1.Rows(0).Height
                e.Graphics.DrawRectangle(Pens.Black, r)
                e.Graphics.DrawString(If(Not DataGridView1.Rows(y).Cells(x).Value Is Nothing, DataGridView1.Rows(y).Cells(x).Value.ToString, ""),
                                        gridFont, Brushes.Black, r, sf)

                startX += r.Width
            Next
            startY += r.Height
            If startY >= e.MarginBounds.Bottom - 10 Then
                If y < DataGridView1.Rows.Count - 1 Then
                    e.HasMorePages = True
                    pageCounter += 1
                    startRow = y + 1
                    Exit For
                End If
            End If
        Next
    End Sub
End Class
MatSnow
  • 7,357
  • 3
  • 19
  • 31
Vivek
  • 5
  • 4
  • Check the value of dateattend after you set it. I suspect it's there. Format the dates as you add them to that string (e.g. Dim dateattend As String = "Date :-" + DateTimePicker1.Value.Date.ToString("dd/MM/yyyy") + " - " + DateTimePicker2.Value.Date.ToString("dd/MM/yyyy") – Jon Roberts Jan 04 '22 at 12:24
  • Actually that is a text for header and I want to change the format in datagridview – Vivek Jan 04 '22 at 12:59
  • In that case it's this line: e.Graphics.DrawString(If(Not DataGridView1.Rows(y).Cells(x).Value Is Nothing, DataGridView1.Rows(y).Cells(x).Value.ToString, ""), gridFont, Brushes.Black, r, sf) - test if the value is a date and, if so, format it as you want it. – Jon Roberts Jan 04 '22 at 13:31
  • Bro I have another question on vb.net can you answer that? – Vivek Jan 16 '22 at 16:03
  • https://stackoverflow.com/questions/70731253/fill-datagridview-based-on-combobox-selected-item-which-depends-on-another-selec – Vivek Jan 16 '22 at 16:03

1 Answers1

0

You have two options. The simple option is to change:

e.Graphics.DrawString(If(Not DataGridView1.Rows(y).Cells(x).Value Is Nothing, DataGridView1.Rows(y).Cells(x).Value.ToString, ""),gridFont, Brushes.Black, r, sf)

to use FormattedValue:

e.Graphics.DrawString(If(Not DataGridView1.Rows(y).Cells(x).Value Is Nothing, DataGridView1.Rows(y).Cells(x).FormattedValue.ToString, ""),
                                    gridFont, Brushes.Black, r, sf)

alternatively, change it to this to catch the dates and format them manually:

Dim cell As DataGridViewCell = DataGridView1.Rows(y).Cells(x)
Dim formatted As String = ""
If cell.Value IsNot Nothing Then
  If cell.ValueType Is GetType(DateTime) Then
      formatted = DirectCast(cell.Value, DateTime).ToString("dd/MM/yyyy")
  Else
      formatted = cell.FormattedValue
  End If
End If
e.Graphics.DrawString(formatted, gridFont, Brushes.Black, r, sf)
Jon Roberts
  • 2,262
  • 2
  • 13
  • 17
  • Thanks for this. The formatted value worked. And also I removed both formatted value and .tostring. then also it works fine. Thanks so much – Vivek Jan 04 '22 at 17:44
  • I have a question in this bro, I just want to know how to display all the pages in which the datagridview data is present during the printdialog – Vivek Jan 18 '22 at 08:17
  • can you help me with this question mate:- https://stackoverflow.com/questions/70768873/change-value-of-selected-records-from-ms-access-database-in-datagridview-in-vb-n – Vivek Jan 19 '22 at 10:28