我有一个数据表,并按特定顺序填充了数据。将数据放入表格后,我需要对其他字段进行一些计算,但是如果有意义的话,我需要从底部开始进行计算?
此刻我提出以下内容:
For Each dr As DataRow In Me.DataSet1.MyDataTable()
Dim parent_warehouse As String = dr("WH")
Dim parent_product As String = dr("ProductCode")
Dim new_material = 0
Dim new_labour = 0
Dim new_overhead = 0
Dim new_outwork = 0
Dim new_total = 0
If isassembly(parent_warehouse, parent_product) Then
If Not IsDBNull(dr("assy")) Then
For Each dr1 As DataRow In Me.DataSet1.MyDataTable()
Dim assy = dr("assy")
If assy = parent_warehouse + parent_product Then
new_material = new_material + (dr1("material") * (dr1("UsageQuantity") / dr1("Wastage")))
new_labour = new_labour + (dr1("labour") * dr1("UsageQuantity"))
new_overhead = new_overhead + (dr1("overhead") * dr1("UsageQuantity"))
new_outwork = new_outwork + (dr1("outwork") * dr1("UsageQuantity"))
new_total = new_material + new_labour + new_overhead + new_outwork
End If
Next
dr("new_material") = new_material
dr("new_labour") = new_labour
dr("new_overhead") = new_overhead
dr("new_outwork") = new_outwork
dr("new_total") = new_total
Else
GoTo skip
End If
Else
dr("new_material") = dr("material")
dr("new_labour") = dr("labour")
dr("new_overhead") = dr("overhead")
dr("new_outwork") = dr("outwork")
dr("new_total") = dr("total")
End If
skip:
Next
我目前遇到的问题是,当它遍历记录时,它会从上到下遍历它们,我希望它从下而上遍历,因为成本应该从底层产品开始累积,因此他们的父产品会获得新的成本,并一直保持最高价格。
答案 0 :(得分:2)
向后循环将是您的最佳选择。您将使用带有' declare a variable for our DataTable
Dim dt As DataTable = Me.DataSet1.MyDataTable()
' we're setting our 'i' variable to the last row (account for 0-index)
' in a typical For Loop, the Step is +1, but we're specifying -1 to go backwards
For i as Integer = dt.Rows.Count - 1 to 0 Step -1
' execute logic
' something like CDec(dt.Rows(i).Item("DecimalColumn")) to convert decimal type
' CInt(), CStr(), CDate() are also very useful, Integer, String, and Date respectively
Next
jQuery( '.add_to_cart_button' ).on( 'click', function($) {
$( 'body' ).trigger( 'update_checkout' );
});
答案 1 :(得分:0)
我认为您需要对表的默认视图进行排序,然后遍历默认视图。
Me.DataSet1.MyDataTable.defaultview.sort = "material", "labor" 'etc.
For Each dr As DataRowView In Me.DataSet1.MyDataTable.defaultview
'...
*根据@jmcilhinney评论中的更正进行编辑。 -谢谢!