[英]How to calculate expiration date with VB.NET and SQL Server using Visual Studio 2019 and SQL SERVER Enterprise 13?
I'm trying to display on a DataGridView Cell, a value to represent the remaing days between two dates and i'm using DateDiff but it returns an empty cell and i don't know what i'm missing or doing wrong.我试图在 DataGridView 单元格上显示一个值来表示两个日期之间的剩余天数,我正在使用 DateDiff 但它返回一个空单元格,我不知道我遗漏了什么或做错了什么。 My Code follows, please help.
我的代码如下,请帮忙。
Thank you.谢谢你。
Private Sub ValidadeLicencas_Load(sender As Object, e As EventArgs) Handles Me.Load Private Sub ValidadeLicencas_Load(sender As Object, e As EventArgs) Handles Me.Load
Conectar()
Dim cmd As New SqlCommand("SELECT AlvaraNumero,NomeEmpresa,ContactoEmpresa,ValidadeLicenca FROM EmissaoLicenca", conexoes)
Dim da As New SqlDataAdapter
da.SelectCommand = cmd
Dim dt As New DataTable
dt.Clear()
da.Fill(dt)
DataGridViewValidade.DataSource = dt
conexoes.Close()
DataGridViewValidade.AllowUserToAddRows = False
Dim ColDias As New DataGridViewTextBoxColumn
ColDias.Name = "drestantes"
ColDias.HeaderText = "Dias Restantes"
ColDias.Width = 150
DataGridViewValidade.Columns.Insert(4, ColDias)
For Each row As DataGridViewRow In DataGridViewValidade.Rows
Dim expdata_v As Date = row.Cells("ValidadeLicenca").Value
Dim calcdias_v As Integer = DateDiff(DateInterval.Year, Date.Now, expdata_v)
row.Cells("drestantes").Value = calcdias_v
Next
End Sub
The result should be something like this:结果应该是这样的:
AlvaraNumero![]() |
NomeEmpresa![]() |
ContactoEmpresa![]() |
ValidadeLicenca![]() |
Dias Restantes![]() |
---|---|---|---|---|
AlvaraNumero1![]() |
NomeEmpresa1![]() |
ContactoEmpresa1![]() |
30/11/2022 ![]() |
8 ![]() |
AlvaraNumero2![]() |
NomeEmpresa2![]() |
ContactoEmpresa2![]() |
25/11/2022 ![]() |
3 ![]() |
But i get this instead.但是我得到了这个。
AlvaraNumero![]() |
NomeEmpresa![]() |
ContactoEmpresa![]() |
ValidadeLicenca![]() |
Dias Restantes![]() |
---|---|---|---|---|
AlvaraNumero1![]() |
NomeEmpresa1![]() |
ContactoEmpresa1![]() |
30/11/2022 ![]() |
|
AlvaraNumero2![]() |
NomeEmpresa2![]() |
ContactoEmpresa2![]() |
25/11/2022 ![]() |
It would be easier to add the date difference calculation into the SQL query.将日期差异计算添加到 SQL 查询中会更容易。
SELECT AlvaraNumero,NomeEmpresa,ContactoEmpresa,ValidadeLicenca, DATEDIFF(YEAR, ValidadeLicenca , GETDATE()) [drestantes] FROM EmissaoLicenca
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.