Issue
i have a program that print a datagridview and have a date column in it.
the column display date only but when i try to print it, it will have time add how can i remove it?
this is how it looks in the datagridview
and this is my printpreview
print code
Try
dgvInventoryLog.Columns(6).DefaultCellStyle.Format = "dd/MM/yyyy"
Dim actualWidth As Integer = dgvInventoryLog.Columns.Cast(Of DataGridViewColumn).Sum(Function(c) c.Width)
Dim percentage As Decimal = CDec(((100 / actualWidth) * e.MarginBounds.Width) / 100)
Dim header As String = "Inventory Log"
Dim footer As String
Dim startX As Integer = e.MarginBounds.Left
Dim startY As Integer = e.MarginBounds.Top
Dim r As Rectangle
Dim headerFont As New Font(dgvInventoryLog.Font.FontFamily, 15, FontStyle.Italic, GraphicsUnit.Pixel)
Dim szf As SizeF = e.Graphics.MeasureString(header, headerFont)
e.Graphics.DrawString(header, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, startY - szf.Height)
footer = "Page " & pageCounter.ToString
szf = e.Graphics.MeasureString(footer, headerFont)
e.Graphics.DrawString(footer, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, e.MarginBounds.Bottom + 5)
startY += 5
'this is the text alignment
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim gridFont As New Font(dgvInventoryLog.Font.FontFamily, dgvInventoryLog.Font.Size, FontStyle.Regular, GraphicsUnit.Pixel)
' If startRow = 0 Then
For x As Integer = 0 To dgvInventoryLog.Columns.Count - 1
r.X = startX
r.Y = startY
r.Width = CInt(dgvInventoryLog.Columns(x).Width * percentage)
r.Height = dgvInventoryLog.Rows(0).Height
e.Graphics.DrawRectangle(Pens.Black, r)
e.Graphics.DrawString(dgvInventoryLog.Columns(x).HeaderText, gridFont, Brushes.Black, r, sf)
startX += r.Width
Next
startY += r.Height
' End If
For y As Integer = startRow To dgvInventoryLog.Rows.Count - 1
If y = dgvInventoryLog.NewRowIndex Then Continue For
startX = e.MarginBounds.Left
For x As Integer = 0 To dgvInventoryLog.Columns.Count - 1
r.X = startX
r.Y = startY
r.Width = CInt(dgvInventoryLog.Columns(x).Width * percentage)
r.Height = dgvInventoryLog.Rows(0).Height
e.Graphics.DrawRectangle(Pens.Black, r)
e.Graphics.DrawString(If(Not dgvInventoryLog.Rows(y).Cells(x).Value Is Nothing, dgvInventoryLog.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 < dgvInventoryLog.Rows.Count - 1 Then
e.HasMorePages = True
pageCounter += 1
startRow = y + 1
Exit For
End If
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
''preview button
pageCounter = 1
startRow = 0
ppd.Document = pd
ppd.WindowState = FormWindowState.Maximized
ppd.ShowDialog()
End Sub
Private Sub getInventoryLog()
Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\INVENTORY_DB.accdb")
Dim cmd As New OleDbCommand("Select ItemCode as [ITEM CODE], ItemName AS [ITEM NAME], Description AS [DESCRIPTION], Price AS [PRICE],
Unit AS [UNIT], Quantity AS [QUANTITY], MovementDate AS [MOVEMENT DATE], `From` AS [FROM],
`To` AS [TO] From [Items Movement]", conn)
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
dgvInventoryLog.Columns.Clear()
dgvInventoryLog.DataSource = dt
dgvInventoryLog.Columns(0).Width = 80
dgvInventoryLog.Columns(1).Width = 250
dgvInventoryLog.Columns(2).Width = 150
dgvInventoryLog.Columns(3).Width = 70
dgvInventoryLog.Columns(4).Width = 70
dgvInventoryLog.Columns(5).Width = 100
dgvInventoryLog.Columns(6).Width = 110
dgvInventoryLog.Columns(7).Width = 135
dgvInventoryLog.Columns(8).Width = 135
End Using
End Sub
this is where i get the code for printing. i dont quite understand it, but its working just fine so i just copy paste it, so if that is where i messed up i will be grateful for pointing it out for me. thanks
https://www.vbforums.com/showthread.php?872037-RESOLVED-Need-help-with-printing-datagridview-content
Solution
Inside your loop, check if it's the 7th column (index 6), then convert the value to date and format it as you wish
For x As Integer = 0 To dgvInventoryLog.Columns.Count - 1
r.X = startX
r.Y = startY
r.Width = CInt(dgvInventoryLog.Columns(x).Width * percentage)
r.Height = dgvInventoryLog.Rows(0).Height
e.Graphics.DrawRectangle(Pens.Black, r)
If x = 6 Then
Dim movementDate As Date = Ctype(dgvInventoryLog.Rows(y).Cells(x).Value, Date)
e.Graphics.DrawString(movementDate.ToString("MM/dd/yyyy"),
gridFont, Brushes.Black, r, sf)
' or dd/MM/yyyy
Else
e.Graphics.DrawString(If(Not dgvInventoryLog.Rows(y).Cells(x).Value Is Nothing, dgvInventoryLog.Rows(y).Cells(x).Value.ToString, ""),
gridFont, Brushes.Black, r, sf)
End If
startX += r.Width
Next
Answered By - F0r3v3r-A-N00b Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.