0

Please help me guys! I want the time in Date_Purchased(date) to be removed in datagridview. Because whenever I exported the datagrid in PDF, it has the date and time in it. I only want the date and remove the time especially when exported to PDf.

Here's the sample piece of code.

     Public Sub NewInventory()
        Dim NI as SqlCommand =con.CreateCommand
        NI.CommandText = "Insert into Items_table(Room_ID, PC_Number, Item_Name, Date_Purhased) VALUES (@Room_ID,@PC_Number, @Item_Name, @Date_Purchased);"

        NI.Parameters.AddWithValue("@Room_ID", Room_ID)
        NI.Parameters.AddWithValue("@PC_Number", PC_Number)
        NI.Parameters.AddWithValue("@Item_Name", Item_Name)
        NI.Parameters.AddWithValue("@Date_Purchased", DatePurchasedDTP.Value)

        NI.ExecuteNonQuery()
        MessageBox.Show("New item created.")
    End Sub

//for DataGridView

     Public Sub GetRoomItems(RoomID As String)
          Dim getItems As String = "Select Item_ID, PC_Number, 
      Item_Name, Date_Purchased WHERE Room_ID=" + RoomID
          Dim adapter As New SqlDataAdapter(getItems, connection)

          Dim table As New DataTable()
          adapter.Fill(table)
          InventoryDataGrid.DataSource = table
     End Sub

//For exporting to pdf

     Private Sub ExportButton_Click(sender As Object, e As  EventArgs) Handles ExportButton.Click
          connection.Close()
          connection.Open()

          Dim pdfTable As New PdfPTable(ReportDataGridView.ColumnCount)
          pdfTable.DefaultCell.Padding = 1
          pdfTable.WidthPercentage = 100
          pdfTable.DefaultCell.HorizontalAlignment = Element. ALIGN_CENTER

          Dim ptable As New  Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11, iTextSharp.text.Font.BOLD, BaseColor.BLACK)

     For Each column As DataGridViewColumn in ReportDataGridView.Columns
          Dim cell as new PdfPCell(New Phrase(New Chunk(column.HeaderText, ptable)))
          cell.HorizontalAlignment = Element.ALIGN_CENTER
          cell.FixedHeight = 30
          pdfTable.AddCell(cell)

     Next

     For Each row as DataGridViewRow In ReportDataGridView.Rows
          For each cell as DataGridViewCell In row.Cells
          pdfTable.AddCell(cell.Value.ToString)
          Next
     Next

         Dim folderpath As String = "C:\PDFs\"
         If Not Directory.Exists(folderpath) Then
              Directory.CreateDirectory(folderpath)
         End If

     Using sfd as New SaveFileDialog()
         sfd.ShowDialog()
         sfd.OverWritePrompt = True
         sfd.Title =" Save As"
         sfd.AddExtension = True
         sfd.DefaultExt = ".pdf"

          Using stream As New FileStream(sfd.FileName & ".pdf", 
FileMode.Create)
          Dim pdfdoc As New Document (PageSize.LETTER, 36.0F, 36.0F,36.0F,36.0F)
          PdfWriter.GetInstance(pdfdoc.stream)
          pdfdoc.Open()
          pdfdoc.Add(pdfTable)
          pdfdoc.Close()
          stream.Close()
          If File.Exists("path") Then
               File.AppendAllText("path", "contents")
     End If
     pdfdoc.Close()
     stream.Close()
          End Using

     End Using
     End Sub

If you would ask me what's the data type of Date_Purchased, it is date. I used date not datetime. And still confused why the time is still in pdf whenever I exported it.

Please help me! Thank you so much

Annie
  • 1
  • 1
  • 1
    I don't see any code here which relates to your DataGridView or exporting to PDF? – WSC Mar 20 '20 at 08:56
  • Hi. I've already edited my code. Please take a look at it. I already included the code which relates to my DataGridView and exporting to PDF. Thank you – Annie Mar 26 '20 at 04:23

1 Answers1

0

In .NET a Date still has a time component, it's just set to midnight (e.g. 12:00:00). You can read about this here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

A new object with the same date as this instance, and the time value set to 00:00:00 midnight (00:00:00).

If you want to display a Date as a string with only the date, you can use Date.ToString() and specify a format which omits the time such as Date.ToString("dd/MM/yyyy"). Alternatively you can use Date.ToShortDateString() which does this but uses the current culture short date string format.

More info here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

In your code you are doing this loop:

For Each row as DataGridViewRow In ReportDataGridView.Rows
    For each cell as DataGridViewCell In row.Cells
        pdfTable.AddCell(cell.Value.ToString)
    Next
Next

If you change this to use the underlying DataTable instead of the DataGridView itself you can use the DataColumn.DataType to check when you're using a date:

For Each row as DataRow In DataTable.Rows
    For Each item In row.ItemArray
        If item.GetType Is GetType("System.DateTime") Then
            pdfTable.AddCell(item.ToString("dd/MM/yyyy"))
        Else
            pdfTable.AddCell(item.Value.ToString)
        End If
    Next
Next

I'm a bit rusty on the specifc syntax but you should be able to work it out from there.

WSC
  • 903
  • 10
  • 30
  • There's an error in Datatable.Rows which says "Reference to a non-shared member requires an object reference" – Annie Apr 03 '20 at 03:47
  • It's not working. It doesn't get the items in DatagridView when I used your code and when I exported it to PDF. – Annie Apr 03 '20 at 04:04
  • You need to replace that pseudo-code with your own code. `DataTable.Rows` needs to be `NameOfYourDataTable.Rows`. – WSC Apr 03 '20 at 08:32