【问题标题】:Remove the time in Datagridview and when exported to PDF删除 Datagridview 中的时间以及导出为 PDF 时的时间
【发布时间】:2020-03-20 07:08:09
【问题描述】:

请帮帮我! 我希望在 datagridview 中删除 Date_Purchased(date) 中的时间。因为每当我以 PDF 格式导出数据网格时,其中都有日期和时间。我只想要日期并删除时间,尤其是在导出到 PDf 时。

这是示例代码。

     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

如果您要问我 Date_Purchased 的数据类型是什么,它是日期。我使用日期而不是日期时间。并且仍然困惑为什么每当我导出它时时间仍然是 pdf 格式。

请帮帮我!非常感谢

【问题讨论】:

  • 我在这里没有看到任何与您的 DataGridView 或导出为 PDF 相关的代码?
  • 嗨。我已经编辑了我的代码。请看一下。我已经包含了与我的 DataGridView 相关的代码并导出为 PDF。谢谢

标签: vb.net sql-server-2017-express


【解决方案1】:

在 .NET 中,Date 仍然有一个时间组件,它只是设置为午夜(例如 12:00:00)。你可以在这里阅读:https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

与此实例具有相同日期和时间值的新对象 设置为午夜 00:00:00 (00:00:00)。

如果要将日期显示为仅包含日期的字符串,可以使用Date.ToString() 并指定省略时间的格式,例如Date.ToString("dd/MM/yyyy")。或者,您可以使用 Date.ToShortDateString() 这样做但使用当前文化短日期字符串格式。

更多信息在这里:https://docs.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

在您的代码中,您正在执行此循环:

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

如果您将其更改为使用基础DataTable 而不是DataGridView 本身,您可以使用DataColumn.DataType 来检查您何时使用日期:

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

我对具体的语法有点生疏,但你应该可以从那里解决。

【讨论】:

  • Datatable.Rows 中有一个错误提示“对非共享成员的引用需要对象引用”
  • 它不工作。当我使用您的代码并将其导出为 PDF 时,它不会获取 DatagridView 中的项目。
  • 你需要用你自己的代码替换那个伪代码。 DataTable.Rows 必须是 NameOfYourDataTable.Rows
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多