请帮助我!我希望在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。
请帮助我!非常感谢
在.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
我对特定语法有点不满意,但是您应该可以从那里解决它。