我正在 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
uj5u.com熱心網友回復:
你有兩個選擇。簡單的選擇是更改:
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)
使用 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)
或者,將其更改為此以捕獲日期并手動格式化它們:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405794.html
標籤:
上一篇:VB.Net中任意字串的Directcast或ctype
下一篇:理解excelCOM中參考的含義
