将计算的剩余时间添加到数据网格视图的每个插入行 [英] add computed remainingTime to each inserted row of a datagridview

查看:26
本文介绍了将计算的剩余时间添加到数据网格视图的每个插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好在我的项目中,我正在使用 vb 并且我有一个 datagridview 与数据源的绑定.例如我有这个列名

Hello on my project I am using vb and I have a datagridview binding with datasource. For example I have this columns name

StartTime- 显示作业开始的时间

StartTime- this show the time a job started

上午 7:00下午 4:30

7:00 am 4:30 pm

小时 - 该作业将运行多长时间

Hours- how long that job is going to run

30 分钟2 小时

我希望能够将计算剩余时间添加到插入到数据网格视图中的每一行中.

I want to be able to add the compute remaining time to each row that is inserted into the datagridview.

我已经有一个未绑定到数据源的列名剩余时间.如何将计算剩余时间添加到每一行.即使用户插入新行?我已经计算了剩余时间,但它只对第一行执行此操作.

I already have a column name remainingTime that is not binding to the datasource. How can i add the compute remainingtime to each row. Even when the user inserted a new row? I already calculate the remainingTime but it only do this for the first row.

在我的表单加载中

Dim myTime As Date
'RemainingTime
    Dim totalTime As Date = runningJobs_list.Rows(0).Cells(5).Value
    ' Dim totalTime As Date = runningJobs_list.Rows(0).Cells(5).Value
    myTime = totalTime.AddHours(runningJobs_list.Rows(0).Cells(6).Value)
    ' myTime = totalTime.AddHours(runningJobs_list.Rows(0).Cells(6).Value)
    Timer.Start()

这是我用来倒计时的计时器

this is my timer to do the countdown of reaming time

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
    'variable to compare two timespan
    Dim myspan As TimeSpan
    myspan = TimeSpan.Zero
    'decrease myTime to actual system time
    Dim remainingTime As TimeSpan = myTime.Subtract(Date.Now)

    'if the remaining time is less than zero then set red the entire row otherwise decrease timer
    If remainingTime < myspan Then
        runningJobs_list.Rows(0).DefaultCellStyle.BackColor = Color.Red
    Else
        runningJobs_list.Rows(0).Cells(8).Value = remainingTime

    End If
End Sub

有人可以帮我吗?

推荐答案

您必须将该计算包装到 foreach 循环中,循环遍历网格中的所有行(最后一个空行除外.检查行是否为 IsNewRow 状态,或者如果 Cells(0).文本为空以验证是否为空...)

You have to wrap that calculation into foreach loop which loops through all rows in grid (except last empty row. Check for rows IsNewRow status, or if Cells(0).Text is empty to validate is it empty or not...)

所以:

runningJobs_list.Rows(0).Cells(8).Value = ...

应该是这样的:

Should be something like this:

ForEach gridRow As DataGridRow in runningJobs_List.Rows
  gridRow.Cells(8).Value = ...<br />
Next

这篇关于将计算的剩余时间添加到数据网格视图的每个插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆