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