首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VB.net按特定顺序遍历记录

VB.net按特定顺序遍历记录
EN

Stack Overflow用户
提问于 2020-08-20 23:21:45
回答 2查看 176关注 0票数 0

我有一个datatable,它以特定的顺序填充数据。一旦数据在表中,我需要对其他字段执行一些计算,但如果有意义的话,我需要从最下面的一行开始计算?

在那一刻,我想到了以下几点:

代码语言:javascript
运行
复制
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

我目前遇到的问题是,当它循环遍历记录时,它会从上到下循环,我希望它从下到上循环,因为成本应该是从底部产品向上累积的,这样它们的父产品就会获得新的成本,并一直向上循环到顶部产品。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-21 18:52:36

反向循环将是您的最佳选择。您将使用带有Step -1的For循环

代码语言:javascript
运行
复制
' 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
票数 2
EN

Stack Overflow用户

发布于 2020-08-20 23:30:52

我认为您需要对表的默认视图进行排序,然后遍历默认视图。

代码语言:javascript
运行
复制
Me.DataSet1.MyDataTable.defaultview.sort = "material", "labor" 'etc.

For Each dr As DataRowView In Me.DataSet1.MyDataTable.defaultview
'...

*根据@jmcilhinney在评论中的更正进行了编辑。--谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63508274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档