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
CodePudding user response:
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
CodePudding user response:
You can make it so that the printing code respects the formatting in all columns, regardless of data type.
Dim value = dgvInventoryLog.Rows(y).Cells(x).Value
Dim format = dgvInventoryLog.Columns(x).DefaultCellStyle.Format
Dim text = If(String.IsNullOrEmpty(format),
value.ToString(),
String.Format($"{{0:{format}}}", value))
e.Graphics.DrawString(text, gridFont, Brushes.Black, r, sf)
That will work for any type and any legitimate format and it will also work with empty cells, whether they contain Nothing
or DBNull.Value
. What gets printed will be the same as what gets displayed in the grid regardless.
This bit:
String.Format($"{{0:{format}}}", value)
might look a bit weird, so let me explain. The $
indicates string interpolation. Anything wrapped in braces within that interpolated string is evaluated and replaced with the result, with literal braces escaped with another brace. If, for instance, format
contained "dd/MM/yyyy"
, $"{{0:{format}}}"
would produce "{0:dd/MM/yyyy}"
. When that gets passed to String.Format
, is is replaced with the contents of value
in the specified format.