开发者

Remove dupe rows from DataTable

开发者 https://www.devze.com 2023-01-15 21:03 出处:网络
_CheckPayees = ds.Tables(\"Payees\") _CheckPayees is a DataTable variable. After it is set with the code above, I would like to go through it and remove duplicate rows from it. These rows have to be
_CheckPayees = ds.Tables("Payees")

_CheckPayees is a DataTable variable. After it is set with the code above, I would like to go through it and remove duplicate rows from it. These rows have to be exactly dupes though, say two columns have matching values however another one doesn't, that's not considered 开发者_开发问答a dupe.

Is there an easy way to do this?


Nevermind....this works.

I hate to answer my own questions but...I figure it I'm stuck on something for a while, and then find it out, and this has no answers yet, I'll post mine because other people may need it to.

Private Function EliminateDuplicates(ByVal dt As DataTable) As DataTable
            Dim ssort As String = ""
            Dim col As DataColumn
            Dim scol As DataColumn
            For Each scol In dt.Columns
                If ssort <> "" Then ssort &= ","
                ssort &= scol.ColumnName
            Next

            dt.DefaultView.Sort = ssort
            Dim i As Integer, bEquals As Boolean, c As Integer, ccount As Integer
            ccount = dt.Columns.Count
            For i = dt.Rows.Count - 1 To 1 Step -1
                bEquals = True
                For c = 0 To ccount - 1
                    If dt.DefaultView(i)(c).ToString() <> dt.DefaultView(i - 1)(c).ToString() Then
                        bEquals = False
                        Exit For
                    End If
                Next c

                If bEquals Then
                    dt.DefaultView(i).Delete()
                End If
            Next

            Return dt

        End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消