我发现很难弄清楚这个概念。场景应该是这样的:
图片是datagridview包含3列(今天,明天和第二天)
让我们说减去时间,但是如果我将在第1列(明天)上午8点进行绘图,那么差异将从17:00(下午5点)开始第0列(今天)
我只需要代码段,因为我已经知道了减法的源代码。
Datagridview:
编辑:为了避免混淆,如果我从第1列的08:00开始减去,如何在Datagridview的今日第0列上绘制14:00。(仅限营业时间从早上8点到下午5点)
EDIT2:请求的源代码:
For i As Integer = 0 To frmSchedule.DatagridSchedule.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(frmSchedule.DatagridSchedule.Rows(i).HeaderCell.Value) Then
frmSchedule.DatagridSchedule.Rows(i).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
frmSchedule.DatagridSchedule.DefaultCellStyle.WrapMode = DataGridViewTriState.True
Dim datetime1 As DateTime = cmbSelectAvailableTIme.Text
Dim newDateTime As DateTime = datetime1.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(0).Value = Info1
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(1).Value = Info2
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(2).Value = Info2
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(3).Value = INfo4
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(4).Value = "Invoice"
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(5).Value = "MP"
StatusBillboard.DataGridViewBillboard.Rows(i).Cells(6).Value = DateTime.Now.AddDays(1).ToString("MM/dd/yyyy") & " " & newDateTime
For i_scan As Integer = 0 To FrmFinalApproval.DatagridFinalApproval.Rows.Count - 1
For i_inspection As Integer = 0 To PreInspection.DatagridInspection.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(FrmFinalApproval.DatagridFinalApproval.Rows(i_scan).HeaderCell.Value) Then
cmbSelectAvailableTIme.Text = ""
cmbSelectAvailableTIme.Text = Format(newDateTime, "HH:mm")
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
FrmFinalApproval.DatagridFinalApproval.DefaultCellStyle.WrapMode = DataGridViewTriState.True
' cell.Style.BackColor = Color.LightSalmon
FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).Style.BackColor = Color.LightYellow
FinalGridview = FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).RowIndex
FInalGridview2 = FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).ColumnIndex
End If
If cmbSelectAvailableTIme.SelectedItem.Equals(PreInspection.DatagridInspection.Rows(i_inspection).HeaderCell.Value) Then
cmbSelectAvailableTIme.Text = ""
cmbSelectAvailableTIme.Text = Format(newDateTime, "HH:mm")
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
PreInspection.DatagridInspection.DefaultCellStyle.WrapMode = DataGridViewTriState.True
PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).Style.BackColor = Color.LightYellow
carwashInspect = PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).RowIndex
Carwashinspect2 = PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).ColumnIndex
End If
Next
Next
Dim datetime2 As DateTime = cmbSelectAvailableTIme.Text
Dim newDateTime2 As DateTime = datetime2.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
cmbSelectAvailableTIme.Text = newDateTime2.ToString("HH:mm")
For i_scan2 As Integer = 0 To FrmCandCollection.DatagridCandCollection.Rows.Count - 1
For i_carwash As Integer = 0 To Carwash.DatagridCarwash.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(Carwash.DatagridCarwash.Rows(i_scan2).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(Carwash.DatagridCarwash.Rows(i_scan2).HeaderCell.Value)
FrmCandCollection.DatagridCandCollection.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
FrmCandCollection.DatagridCandCollection.DefaultCellStyle.WrapMode = DataGridViewTriState.True
CandCGridview = FrmCandCollection.DatagridCandCollection.Rows(selectedIndex).Cells(0).RowIndex
CandCGridview2 = FrmCandCollection.DatagridCandCollection.Rows(selectedIndex).Cells(0).ColumnIndex
End If
If cmbSelectAvailableTIme.SelectedItem.Equals(Carwash.DatagridCarwash.Rows(i_carwash).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(Carwash.DatagridCarwash.Rows(i_carwash).HeaderCell.Value)
Carwash.DatagridCarwash.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
Carwash.DatagridCarwash.DefaultCellStyle.WrapMode = DataGridViewTriState.True
CarwashGridview = Carwash.DatagridCarwash.Rows(selectedIndex).Cells(0).RowIndex
CarwashGridview2 = Carwash.DatagridCarwash.Rows(selectedIndex).Cells(0).ColumnIndex
End If
Next
Next
Dim datetime3 As DateTime = cmbSelectAvailableTIme.Text
Dim newDateTime3 As DateTime = datetime3.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
cmbSelectAvailableTIme.Text = newDateTime3.ToString("HH:mm")
For i_scan3 As Integer = 0 To frmAccounting.DatagridAccounting.Rows.Count - 1
For i_caraccessories As Integer = 0 To frmAccessories.DatagridviewAccessories.Rows.Count - 1
For i_cashier As Integer = 0 To frmCashier.DatagridCashier.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(frmAccounting.DatagridAccounting.Rows(i_scan3).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(frmAccounting.DatagridAccounting.Rows(i_scan3).HeaderCell.Value)
frmAccounting.DatagridAccounting.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
frmAccounting.DatagridAccounting.DefaultCellStyle.WrapMode = DataGridViewTriState.True
AccountGridview = frmAccounting.DatagridAccounting.Rows(selectedIndex).Cells(0).RowIndex
AccountGridView2 = frmAccounting.DatagridAccounting.Rows(selectedIndex).Cells(0).ColumnIndex
End If
If cmbSelectAvailableTIme.SelectedItem.Equals(frmAccessories.DatagridviewAccessories.Rows(i_caraccessories).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(frmAccessories.DatagridviewAccessories.Rows(i_caraccessories).HeaderCell.Value)
frmAccessories.DatagridviewAccessories.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
frmAccessories.DatagridviewAccessories.DefaultCellStyle.WrapMode = DataGridViewTriState.True
AccessoriesGridview = frmAccessories.DatagridviewAccessories.Rows(selectedIndex).Cells(0).RowIndex
AccessoriesGridview2 = frmAccessories.DatagridviewAccessories.Rows(selectedIndex).Cells(0).ColumnIndex
End If
If cmbSelectAvailableTIme.SelectedItem.Equals(frmCashier.DatagridCashier.Rows(i_cashier).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(frmCashier.DatagridCashier.Rows(i_cashier).HeaderCell.Value)
frmCashier.DatagridCashier.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
frmCashier.DatagridCashier.DefaultCellStyle.WrapMode = DataGridViewTriState.True
AccessoriesGridview = frmCashier.DatagridCashier.Rows(selectedIndex).Cells(0).RowIndex
AccessoriesGridview2 = frmCashier.DatagridCashier.Rows(selectedIndex).Cells(0).ColumnIndex
End If
Next
Next
Next
Dim datetime4 As DateTime = cmbSelectAvailableTIme.Text
Dim newDateTime4 As DateTime = datetime4.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
cmbSelectAvailableTIme.Text = newDateTime4.ToString("HH:mm")
For i_scan4 As Integer = 0 To FrmInsurance.DatagridInsurance.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(FrmInsurance.DatagridInsurance.Rows(i_scan4).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
FrmInsurance.DatagridInsurance.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
FrmInsurance.DatagridInsurance.DefaultCellStyle.WrapMode = DataGridViewTriState.True
InsuranceGridView = FrmInsurance.DatagridInsurance.Rows(selectedIndex).Cells(0).RowIndex
InsuranceGridview2 = FrmInsurance.DatagridInsurance.Rows(selectedIndex).Cells(0).ColumnIndex
End If
Next
Dim datetime5 As DateTime = cmbSelectAvailableTIme.Text
Dim newDateTime5 As DateTime = datetime5.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
cmbSelectAvailableTIme.Text = newDateTime5.ToString("HH:mm")
For i_scan5 As Integer = 0 To frmFinancing.DatagridFinancing.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(frmFinancing.DatagridFinancing.Rows(i_scan5).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
frmFinancing.DatagridFinancing.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
frmFinancing.DatagridFinancing.DefaultCellStyle.WrapMode = DataGridViewTriState.True
FinancingGridView = frmFinancing.DatagridFinancing.Rows(selectedIndex).Cells(0).RowIndex
FinancingGRidview2 = frmFinancing.DatagridFinancing.Rows(selectedIndex).Cells(0).ColumnIndex
End If
Next
Dim datetime6 As DateTime = cmbSelectAvailableTIme.Text
Dim newDateTime6 As DateTime = datetime6.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
cmbSelectAvailableTIme.Text = newDateTime6.ToString("HH:mm")
For i_scan6 As Integer = 0 To frmINvoice.DatagridInvoice.Rows.Count - 1
If cmbSelectAvailableTIme.SelectedItem.Equals(frmINvoice.DatagridInvoice.Rows(i_scan6).HeaderCell.Value) Then
Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
frmINvoice.DatagridInvoice.Rows(selectedIndex).Cells(0).Value = Info1 & Environment.NewLine & Info2 & Environment.NewLine & Info3 & Environment.NewLine & txtboxAccessories.Text.ToString & Environment.NewLine & INfo4
frmINvoice.DatagridInvoice.DefaultCellStyle.WrapMode = DataGridViewTriState.True
InvoiceGridView = frmINvoice.DatagridInvoice.Rows(selectedIndex).Cells(0).RowIndex
InvoiceGridView2 = frmINvoice.DatagridInvoice.Rows(selectedIndex).Cells(0).ColumnIndex
End If
Next
End If
Next
答案 0 :(得分:0)
根据营业时间在一天结束并从下一天开始,只需要检查新时间是否在第二天工作时间开始之前,如果是,则减去额外的小时数。
这是一个例子
结束时间:09:00 时间估计:05:00(5小时)
从结束时间减去5小时以获得开始时间 - 04:00
那天营业时间前是04:00吗?是
如果营业时间是08:00至17:00,则减去非营业时间。
此代码应该这样做。
Dim duration As New TimeSpan
Dim startTimeOfBusinessHours As Integer = 8
Dim endTimeOfBusinessHours As Integer = 17
'calculate the number of hours between end and start of business hours
Dim nonBusinessHourseBetweeDays As Integer = 24 + startTimeOfBusinessHours - endTimeOfBusinessHours
'parse duration as timespan
duration = TimeSpan.Parse(TimeEstimate.Text)
'create datetime for start of business hours of the date in DateTime1
Dim businessHoursStart As Date = New Date(datetime1.Year, datetime1.Month, datetime1.Day, startTimeOfBusinessHours, 0, 0, 0)
'calculate the Start time for the plot
Dim newDateTime As DateTime = datetime1.Subtract(duration)
'If the start time of the plot is before the start of busines hours on the end day then subtract
'the number of non business hourse
If newDateTime < businessHoursStart Then
newDateTime.Subtract(New TimeSpan(nonBusinessHourseBetweeDays, 0, 0))
End If
如果您的工作周不是7天,那么您还需要添加更多代码以允许非工作日。