DateAdd在迭代中不更新

时间:2019-07-02 20:40:49

标签: vb.net visual-studio-2012

大家好,我在为一个项目的制造计划员工作时遇到一个问题,在这个项目中,我有一个TextBox1作为开始日期,一个TextBox4作为卷轴数量= Ci,用户必须指定多少Ci(卷轴)将并从那里开始制造程序的计划。

所有这些信息都被清空到DataGridView中,在其中我有“部门”为“部门”,“操作”,“结束日期”,“初始日期”的列

问题是,当我的代码以2号卷轴开头时,它为我分配了1号卷轴所计算的相同日期。

Manufacturing   Channel Cut plate C1            04/07/2019  04/07/2019
Manufacturing   Channel Rolled plate C1         04/07/2019  04/07/2019
Manufacturing   Channel Longituidal welding C1  08/07/2019  09/07/2019
Manufacturing   Channel Re rolled C1            10/07/2019  11/07/2019
Manufacturing   Channel Cut plate C2            04/07/2019  04/07/2019
Manufacturing   Channel Rolled plate C2         04/07/2019  04/07/2019
Manufacturing   Channel Longituidal welding C2  08/07/2019  09/07/2019
Manufacturing   Chanel  rolled C2               10/07/2019  11/07/2019

我需要C2的Reel从Reel 1的上一次活动日期开始并保持不变。

Manufacturing   Channel Cut plate C1            04/07/2019  04/07/2019
Manufacturing   Channel Rolled plate C1         05/07/2019  05/07/2019
Manufacturing   Channel Longituidal welding C1  06/07/2019  07/07/2019
Manufacturing   Channel Re rolled C1            10/07/2019  11/07/2019
Manufacturing   Channel Cut plate C2            11/07/2019  12/07/2019
Manufacturing   Channel Rolled plate C2         13/07/2019  14/07/2019
Manufacturing   Channel Longituidal welding C2  15/07/2019  16/07/2019
Manufacturing   Chanel  rolled C2               17/07/2019  18/07/2019

我正在使用的代码是下一个。

Private Sub Button1_Click (sender As Object, and As EventArgs) Handles Button1.Click
       Dim date1 As Date = CType (TextBox1.Text, Date)
        Dim numRows As Integer
        If Integer.TryParse (TextBox4.Text, numRows) Then
        If (numRows> = 1) Then
        For i As Integer = 1 To numRows
        Table.Rows.Add ("Manufacturing", "Channel", "Cut plate C" & i, DateAdd (DateInterval.Day, 1, date1), DateAdd (DateInterval.Day, 2, date1))
        Table.Rows.Add ("Manufacturing", "Channel", "Rolled plate C" & i, DateAdd (DateInterval.Day, 3, date1), DateAdd (DateInterval.Day, 4, date1))
        Table.Rows.Add ("Manufacturing", "Channel", "Longituid welding C" & i, DateAdd (DateInterval.Day, 5, date1), DateAdd (DateInterval.Day, 6, date1))
        Table.Rows.Add ("Manufacturing", "Channel", "Re rolled C" & i, DateAdd (DateInterval.Day, 7, date1), DateAdd (DateInterval.Day, 8, date1))
        Next
        Else
        MessageBox.Show ("Value:" & TextBox4.Text, "The number of rows must be greater than or equal to 1")
        End If
        Else
        MessageBox.Show ("Value:" & TextBox4.Text, "Invalid number")
        End If
End Sub

1 个答案:

答案 0 :(得分:0)

编辑后的帖子更好。

您说:

  

我需要C2的卷轴从上次活动的日期开始   卷轴1 ...

您可以在循环的底部更改“ date1”的值,以便下一个卷轴以该日期开始:

For i As Integer = 1 To numRows
    Table.Rows.Add("Manufacturing", "Channel", "Cut plate C" & i, DateAdd(DateInterval.Day, 1, date1), DateAdd(DateInterval.Day, 2, date1))
    Table.Rows.Add("Manufacturing", "Channel", "Rolled plate C" & i, DateAdd(DateInterval.Day, 3, date1), DateAdd(DateInterval.Day, 4, date1))
    Table.Rows.Add("Manufacturing", "Channel", "Longituid welding C" & i, DateAdd(DateInterval.Day, 5, date1), DateAdd(DateInterval.Day, 6, date1))
    Table.Rows.Add("Manufacturing", "Channel", "Re rolled C" & i, DateAdd(DateInterval.Day, 7, date1), DateAdd(DateInterval.Day, 8, date1))
    date1 = DateAdd(DateInterval.Day, 8, date1)
Next