Vb.net 自动计算datagridview
如何在datagridview中自动计算列 类似于在microsoft excel中将公式设置到列中 我有一个日期雇佣栏和任期栏。任期列应使用Vb.net 自动计算datagridview,vb.net,datagridview,Vb.net,Datagridview,如何在datagridview中自动计算列 类似于在microsoft excel中将公式设置到列中 我有一个日期雇佣栏和任期栏。任期列应使用DateHired-DateToday公式自动计算 请帮忙。谢谢。您可以将GridView中的偶数“CellEndEdit”处理为: Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load DataGridView1.Columns.Ad
DateHired
-DateToday
公式自动计算
请帮忙。谢谢。您可以将GridView中的偶数
“CellEndEdit”
处理为:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("DateHired", "DateHired")
DataGridView1.Columns.Add("tenure", "tenure")
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Try
Dim order As DataGridView = DirectCast(sender, DataGridView)
If IsDate(order("DateHired", e.RowIndex).Value) Then
'Dim dt As Date = order("DateHired", e.RowIndex).Value
Dim dt1 As Date = order("DateHired", e.RowIndex).Value
Dim dt2 As Date = Date.Parse(DateTime.Now)
' order("tenure", e.RowIndex).Value = DateDiff(DateInterval.Year, dt, Date.Now)
order("tenure", e.RowIndex).Value = Math.Abs(CInt(dt1.Subtract(dt2).TotalDays / 30))
End If
Catch ex As Exception
End Try
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
Dim dt1 As Date = Date.Parse(DateTimePicker1.Text)
Dim dt2 As Date = Date.Parse(DateTime.Now)
txtTenure.Text = Math.Abs(CInt(dt1.Subtract(dt2).TotalDays / 30))
End Sub
您可以将GridView中的偶数
“CellEndEdit”
处理为:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("DateHired", "DateHired")
DataGridView1.Columns.Add("tenure", "tenure")
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Try
Dim order As DataGridView = DirectCast(sender, DataGridView)
If IsDate(order("DateHired", e.RowIndex).Value) Then
'Dim dt As Date = order("DateHired", e.RowIndex).Value
Dim dt1 As Date = order("DateHired", e.RowIndex).Value
Dim dt2 As Date = Date.Parse(DateTime.Now)
' order("tenure", e.RowIndex).Value = DateDiff(DateInterval.Year, dt, Date.Now)
order("tenure", e.RowIndex).Value = Math.Abs(CInt(dt1.Subtract(dt2).TotalDays / 30))
End If
Catch ex As Exception
End Try
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
Dim dt1 As Date = Date.Parse(DateTimePicker1.Text)
Dim dt2 As Date = Date.Parse(DateTime.Now)
txtTenure.Text = Math.Abs(CInt(dt1.Subtract(dt2).TotalDays / 30))
End Sub
您可以使用以下语句来计算两个日期之间的年/月/日以及更多
LblDateDiff.Text = DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTimePicker2.Value)
您可以传递任何有效的日期来代替DateTimePicker1.value和DateTimePicker2.value
您可以使用其他选项代替DateInterval
- 一天
- 每日
- 时辰
- 分钟
- 月
- 四分之一
- 第二
- 平日
- 周末
- 年
Dim ds As New DataSet
qry = "select * from TblDate"
Dim da As New SqlDataAdapter(qry, con)
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Columns.Add("Days", "Days")
For i = 0 To DataGridView1.Rows.Count - 1
With DataGridView1.Rows(i)
.Cells(1).Value = DateDiff(DateInterval.Day, .Cells(0).Value, Today.Date)
End With
Next
在上述示例中,您将获得图像中提到的输出,当前日期为2016年5月7日
您可以使用以下语句计算两个日期之间的年/月/日以及更多
LblDateDiff.Text = DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTimePicker2.Value)
您可以传递任何有效的日期来代替DateTimePicker1.value和DateTimePicker2.value
您可以使用其他选项代替DateInterval
- 一天
- 每日
- 时辰
- 分钟
- 月
- 四分之一
- 第二
- 平日
- 周末
- 年
Dim ds As New DataSet
qry = "select * from TblDate"
Dim da As New SqlDataAdapter(qry, con)
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Columns.Add("Days", "Days")
For i = 0 To DataGridView1.Rows.Count - 1
With DataGridView1.Rows(i)
.Cells(1).Value = DateDiff(DateInterval.Day, .Cells(0).Value, Today.Date)
End With
Next
在上述示例中,您将获得图像中提到的输出,当前日期为2016年5月7日
欢迎使用堆栈溢出!此网站不是代码编写服务。请花一点时间重新阅读中的提问指南。如果您是或合理地可以使用
数据表
作为数据源,那么您只需设置数据列
的表达式
属性即可?这会不会造成一个负面的日期,或者至少会出界?这就像拿了5-20
而不是可能想要的20-5
。不,它没有。我已经得到了公式,但问题是我不知道如何处理datagridview。欢迎使用堆栈溢出!此网站不是代码编写服务。请花一点时间重新阅读中的提问指南。如果您是或合理地可以使用数据表
作为数据源,那么您只需设置数据列
的表达式
属性即可?这会不会造成一个负面的日期,或者至少会出界?这就像拿了5-20
而不是可能想要的20-5
。不,它没有。我已经得到了公式,但问题是我不知道如何处理datagridview。谢谢,但它不起作用(我在textbox中临时使用了这个公式,它起了作用,但我需要的是在Datagridview列中自动计算它。私有子DateTimePicker1_ValueChanged(发送者作为对象,e作为事件参数)处理DateTimePicker1.ValueChanged Dim dt1作为Date=Date.Parse(DateTimePicker1.Text)Dim dt2作为Date=Date.Parse(DateTime.Now)txtRequirement.Text=Math.Abs(CInt(dt1.Subtract(dt2.TotalDays/30))结束Sub,任期为月数。好的,我编辑了我的答案,告诉我他是否为你工作。它工作了,但我必须多次单击每个单元格才能显示答案。谢谢,但它没有工作(我在textbox中临时使用了这个公式,它起了作用,但我需要的是在Datagridview列中自动计算它。私有子DateTimePicker1_ValueChanged(发送者作为对象,e作为事件参数)处理DateTimePicker1.ValueChanged Dim dt1作为Date=Date.Parse(DateTimePicker1.Text)Dim dt2作为Date=Date.Parse(DateTime.Now)txtLexture.Text=Math.Abs(CInt(dt1.Subtract(dt2.TotalDays/30))结束Sub,任期为月数。好的,我已经编辑了我的答案,告诉我他是否为你工作。它起作用了,但我必须多次单击每个单元格才能显示答案。我应该把它放在哪里?我尝试将它放在cellendedit句柄中,但没有起作用。我添加了另一个显示代码和结果的图像。别忘了投票和修改k请回答我应该把它放在哪里?我试图把它放在cellendedit句柄中,但它不起作用。我添加了另一个显示代码和结果的图像。请不要忘记投票并标记答案