打印时仅在 datagridview 中显示日期

Display only date in datagridview while printing

我正在 vb.net 中构建一个考勤管理项目。它将过滤两个日期之间的学生数据并显示出勤率。我正在使用 ms access 2007 数据库。当我将数据库中的日期显示到 datagridview 列时,它只显示我想要的日期,但是当我打印 gridview 时,它显示日期以及 00:00:00,这是不正确的。请帮我解决一下这个。我教授想要,他自己也不知道怎么解决

我在这里添加完整的代码:

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

你有两个选择。简单的选项是更改:

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)

使用格式化值:

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)

或者,将其更改为此以捕获日期并手动设置格式:

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)