datagridview中的vb.net计算
我有一个数据网格视图,在列“2”(alarmtime)中添加了时间,如下图所示 顶部有一个标签(标签9)。如果标签文本显示为“星期五”,则“报警时间”单元格值应增加10分钟。 例如,如果标签显示星期五,则时间应从datagridview中的vb.net计算,vb.net,datagridview,Vb.net,Datagridview,我有一个数据网格视图,在列“2”(alarmtime)中添加了时间,如下图所示 顶部有一个标签(标签9)。如果标签文本显示为“星期五”,则“报警时间”单元格值应增加10分钟。 例如,如果标签显示星期五,则时间应从16:10:00更改为16:20:00 我的代码: Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim myform As New Form2 DataGridView1
16:10:00
更改为16:20:00
我的代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim myform As New Form2
DataGridView1.Columns(2).DefaultCellStyle.Format = "h:mm:ss"
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("AlarmTime").Value = Me.MetroLabel2.Text And Me.DataGridView1.Rows(i).Cells("Frequency").Value = "Weekday" Then
myform.Show()
myform.msgdisply.Text = Me.DataGridView1.Rows(i).Cells("UpdateName").Value
If Me.MetroLabel9.Text = "FRIDAY" Then
Dim iCell1 As String
Dim dt As DateTime
iCell1 = Me.DataGridView1.Rows(i).Cells("AlarmTime").Value
dt = Format(DateTime.Parse(iCell1), "h:mm:ss")
dt.AddMinutes(10)
Me.DataGridView1.Rows(i).Cells("AlarmTime").Value = dt.ToString
End If
next
试试这个:
Dim dt as datetime
dt = datetime.parse(iCell1)
dt.addminutes(10)
Me.DataGridView1.Rows(i).Cells("AlarmTime").Value = dt.ToString("HH:mm")
您可以查看
addminutes
方法信息CellFormatting事件非常适合此操作。与迭代行相比,您还应该启用选项Strict
,因为icellResult=iCell1+“10”
毫无意义<代码>“10”不是整数数据网格视图没有udpating:(我已在计时器刻度中放置,这是详细代码。(参见上文)您确定计时器
正在启动吗?使用调试.Print
计算Me.DataGridView1.Rows(i).Cells(“AlarmTime”).Value
并确保值与预期值一致。感谢Dave,我在将单元格格式设置为“h:mm:ss”时遇到了问题。它也带有日期(例如:1990.10.01,6:10:00)。如何将单元格格式设置为仅限时间我已更新了代码。您应该指定将单元格与之进行比较的时间的字符串格式。