DataTable选择借方和贷方匹配的行

时间:2013-10-15 00:12:05

标签: wpf datagrid datatable

DataTable具有以下列的位置

帐号(整数)(唯一)

借记(十进制)

信用(十进制)

选中(布尔)

可能有多个行具有相同的帐号以及借记条目或信用条目。我试图做的是匹配借记和积分,它们加起来相同的值,并通过遍历所有行将它们标记为选中。有关实现这一目标的最佳方法的任何想法吗?

由于

e.g。

DateTable (in WPF DataGrid)

SQL

strSQL = "SELECT A_Sales_Ledger.Transaction_ID as 'Transaction', "
        strSQL += "Customers.Cust_No as 'Acct', "
        strSQL += "Customers.Cust_Name as 'Name', "
        strSQL += "Customers.Add1 as 'Unit', "
        strSQL += "A_Sales_Ledger.Debit as 'Debit', "
        strSQL += "A_Sales_Ledger.Credit as 'Credit', "
        strSQL += "A_Sales_Ledger.Document_Date as 'Date', "
        strSQL += "A_Sales_Ledger.S_Description as 'Description' "
        strSQL += "FROM A_Sales_Ledger "
        strSQL += "JOIN Customers ON Customers.Customer_ID = A_Sales_Ledger.Customer_ID "
        strSQL += "WHERE A_Sales_Ledger.Paid = 'N' "
        strSQL += "ORDER BY Customers.Cust_No"

1 个答案:

答案 0 :(得分:0)

这可能不是最佳解决方案,但似乎确实有效......

我通过DataTable进行了三次传球

通过一个 - 如果借方总额等于每个客户的贷方总额,则将其标记为已选择

传递两个 - 为客户添加信用总额并迭代借方,直到达到相同的值并将其标记为已选择

通过三次 - 清除与同一客户的借方条目匹配的任何剩余信用条目......

             For Each Row As DataRow In BalanceDT.Rows
            Dim vAcct As String = Row("Acct")
            Dim vDebits As Decimal = BalanceDT.Compute("SUM(Debit)", "Acct = '" & vAcct & "'")
            Dim vCredits As Decimal = BalanceDT.Compute("SUM(Credit)", "Acct = '" & vAcct & "'")
            If vDebits = vCredits Then
                Row("Selected") = True
            End If
        Next


                    Dim CurrentCust As String = ""
        Dim TransactionDT As New DataTable
        Dim ExitFor As Boolean = False
        With TransactionDT.Columns
            .Add("ID", GetType(Integer))
        End With
        For Each Row As DataRow In BalanceDT.Rows
            Dim vAcct As String = Row("Acct")
            ExitFor = False
            TransactionDT.Rows.Clear()
            If Not CurrentCust = vAcct Then
                Dim vCredits As Decimal = BalanceDT.Compute("SUM(Credit)", "Acct = '" & vAcct & "'")
                Dim vSelected() As DataRow = BalanceDT.Select("Acct = '" & vAcct & "'", Nothing)
                For Each SubRow As DataRow In vSelected
                    If SubRow("Selected") = False Then
                        vCredits -= SubRow("Debit")
                        With TransactionDT.Rows
                            .Add(SubRow("Transaction"))
                        End With
                        If vCredits = 0 Then
                            ExitFor = True
                            Exit For
                        End If
                    End If
                Next
                If ExitFor = True Then
                    For Each SubRow As DataRow In vSelected
                        If SubRow("Credit") > 0 Then
                            SubRow("Selected") = True
                        End If
                        If SubRow("Debit") > 0 Then
                            For Each CustomerRow As DataRow In TransactionDT.Rows
                                If CustomerRow("ID") = SubRow("Transaction") Then
                                    SubRow("Selected") = True
                                End If
                            Next
                        End If
                    Next
                End If
            End If
            CurrentCust = vAcct
        Next

                    For Each Row As DataRow In BalanceDT.Rows
            Dim vAcct As String = Row("Acct")
            If Not CurrentCust = vAcct Then
                Dim vSelected() As DataRow = BalanceDT.Select("Acct = '" & vAcct & "' AND Selected = 'False'", "Credit")
                For Each SubRow As DataRow In vSelected
                    If SubRow("Selected") = False Then
                        Dim vCredit As Decimal = SubRow("Credit")
                        For Each CustomerRow In vSelected
                            If CustomerRow("Selected") = False Then
                                Dim vDebit As Decimal = CustomerRow("Debit")
                                If Not vDebit = 0 And Not vCredit = 0 Then
                                    If vDebit = vCredit Then
                                        With TransactionDT.Rows
                                            CustomerRow("Selected") = True
                                            SubRow("Selected") = True
                                        End With
                                        Exit For
                                    End If
                                End If
                            End If
                        Next
                    End If
                Next
            End If
            CurrentCust = vAcct
        Next