Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataViewRowState Problems

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
I am having real troubles trying to compare my original values and the changed values in a DataTable. I use the DataViewRowState enumeration to filter the appropriate values. I do see that the values are different (when comparing the before/after) in their respective DataView, but when I try to copy the set of values to a new DataTable or new DataView, or whatever object, I only get the modified value. Can someone PLEASE help me with this problem, provide me with any tips to steer me in the right direction, anything at all. This is killing me here.

I've provided my code below for your review. Thanks a ton!!

Code:
    Private Sub PrintDifferences(ByVal dv As DataView, ByVal label As String)

        dv.RowStateFilter = DataViewRowState.OriginalRows
        Dim dvOrig As IEnumerator = dv.GetEnumerator

        Dim dtOriginal As New DataTable()
        dtOriginal = dv.Table.Clone
        Dim row As DataRow
        While dvOrig.MoveNext
            row = dtOriginal.NewRow()
            row("LINE_ID") = CType(dvOrig.Current, DataRowView).Row.Item("LINE_ID")
            row("ITEM_VALUE") = CType(dvOrig.Current, DataRowView).Row.Item("ITEM_VALUE")
            dtOriginal.Rows.Add(row)
        End While
        dtOriginal.AcceptChanges()

        dv.RowStateFilter = DataViewRowState.ModifiedCurrent
        Dim dvMod As IEnumerator = dv.GetEnumerator

        Dim dtModified As New DataTable()
        dtModified = dv.Table.Clone
        While dvMod.MoveNext
            row = dtModified.NewRow()
            row("LINE_ID") = CType(dvMod.Current, DataRowView).Row.Item("LINE_ID")
            row("ITEM_VALUE") = CType(dvMod.Current, DataRowView).Row.Item("ITEM_VALUE")
            dtModified.Rows.Add(row)
        End While
        dtModified.AcceptChanges()

        Console.WriteLine(ControlChars.Cr + label)
        Dim i As Integer
        For i = 0 To dtModified.Rows.Count - 1
            Dim original As Long = CType(dtOriginal.Rows(i)("ITEM_VALUE"), Long)
            Dim modified As Long = CType(dtModified.Rows(i)("ITEM_VALUE"), Long)
            Dim diff As Long = Math.Abs(original - modified)
            If diff > 0 Then
                Console.Write("LINE ID ")
                Console.Write(dtModified.Rows(i)("LINE_ID"))
                Console.Write(" = ")
                Console.WriteLine(diff.ToString + " (DIFF)")
            End If
        Next i
        Console.WriteLine(ControlChars.Cr + "======================")

    End Sub

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Ok, I fixed my own problem, but for those out there that may need to do something similar, here is a solution. Any simpler way is welcome. Thanks

Code:
    Private dt As New DataTable()
    Private dv As New DataView(dt)
    Private dtChangeLog As New DataTable()

    Sub Main
        Dim i As Integer
        AddHandler dt.RowChanged, AddressOf OnRowChange

        Dim dc1 As New DataColumn("LINE_ID")
        Dim dc2 As New DataColumn("ITEM_VALUE")
        dt.Columns.Add(dc1)
        dt.Columns.Add(dc2)
        Dim stmtRow As DataRow
        For i = 1 To 5
            stmtRow = dt.NewRow()
            stmtRow("LINE_ID") = i
            stmtRow("ITEM_VALUE") = 0
            dt.Rows.Add(stmtRow)
        Next i
        dt.AcceptChanges()
        dtChangeLog = dt.Clone()

        dt.Rows(0)("ITEM_VALUE") = 350
        dt.Rows(1)("ITEM_VALUE") = 10
        dt.Rows(2)("ITEM_VALUE") = 25
        dt.Rows(3)("ITEM_VALUE") = 0

    End Sub

    Sub OnRowChange(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)

        Dim lineId As Integer = e.Row.Item("LINE_ID")
        dv.RowFilter = "LINE_ID = " & lineId.ToString
        dv.RowStateFilter = DataViewRowState.ModifiedOriginal
        If dv.Count > 0 Then
            Dim original As Long = CType(dv(0)("ITEM_VALUE"), Long)
            Dim modified As Long = CType(e.Row.Item("ITEM_VALUE"), Long)
            Dim diff As Long
            If original <> modified Then
                diff = Math.Abs(original - modified)
                Dim row As DataRow = dtChangeLog.NewRow
                row("LINE_ID") = lineId
                row("ITEM_VALUE") = diff
                dtChangeLog.Rows.Add(row)

                Console.WriteLine(ControlChars.Cr + "======================")
                Console.Write("LINE ID = " & lineId.ToString & "; ")
                Console.Write("OLDValue = " & original.ToString & "; ")
                Console.Write("NEWValue = " & modified.ToString & "; ")
                Console.WriteLine("DIFF = " & diff.ToString & "; ")
                Console.WriteLine("======================")
            End If
        End If

    End Sub

CONSOLE OUTPUT:
======================
LINE ID = 1; OLDValue = 0; NEWValue = 350; DIFF = 350; 
======================
======================
LINE ID = 2; OLDValue = 0; NEWValue = 10; DIFF = 10; 
======================
======================
LINE ID = 3; OLDValue = 0; NEWValue = 25; DIFF = 25; 
======================

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top