Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Is there a way to change the original value in the DataRow?

Is there a way to change the original value in the DataRow?

Scheduled Pinned Locked Moved .NET (Core and Framework)
comdebuggingxmlquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NewSilence
    wrote on last edited by
    #1

    Is there any way to change the original value in the DataRow? DataRow.Item(0) may have modified/current value and its original value. I need to change its original value like row.Item("ColumnName", DataRowVersion.Original) = "NewValue". But this property is for read only any. Ideas? I have found the way but I don't like it and it looks like a hack. Here's the code: Public Function SetRowOriginalItemValue(ByVal primaryKeyColumn As DataColumn, ByVal key As Object, ByVal editColumn As DataColumn, ByVal value As Object) As Boolean 'Utils.Assert.ArgumentCheckForNullReference(primaryKeyColumn, "primaryKeyColumn") 'Utils.Assert.ArgumentCheckForNullReference(key, "key") 'Utils.Assert.ArgumentCheckForNullReference(editColumn, "editColumn") 'Utils.Assert.ArgumentCheckExpectedType(key, primaryKeyColumn.DataType) 'Utils.Assert.ArgumentCheckExpectedType(value, editColumn.DataType) Dim t As DataTable = editColumn.Table Dim d As DataSet = t.DataSet Dim s As New IO.MemoryStream d.WriteXml(s, XmlWriteMode.DiffGram) s.Seek(0, IO.SeekOrigin.Begin) Dim doc As New Xml.XmlDocument doc.Load(s) Dim nsmgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(doc.NameTable) nsmgr.AddNamespace("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1") nsmgr.AddNamespace("my", d.Namespace) Dim xp As String = String.Format(New Globalization.CultureInfo("en-US"), _ "/diffgr:diffgram/diffgr:before/my:{0}[starts-with(@diffgr:id, '{0}')]/my:{1}[text()='{2}']/../my:{3}/node()", _ t.TableName, primaryKeyColumn.ColumnName, key, editColumn.ColumnName) Dim n As Xml.XmlNode = doc.DocumentElement.SelectSingleNode(xp, nsmgr) If Not n Is Nothing Then n.Value = value d.Clear() d.ReadXml(New IO.StringReader(doc.InnerXml), XmlReadMode.DiffGram) Return True End If Return False End Function '''''''''''''''''''' Sub SetRowOriginalItemValueTest() Dim d As New DistrictSet Dim row As DistrictSet.DistrictsRow row = d.Districts.NewDistrictsRow row.DistrictId = 109901 row.DistrictName = "Orange County" d.Districts.AddDistrictsRow(row) d.AcceptChanges() d.Districts(0).DistrictName = "West Coast" Diagnostics.Trace.WriteLine("Current: " & d.Districts.Item(0)("DistrictName")) Diagnostics.Trace.WriteLine("Original before SetRowOriginalItemValue(): " & d.Districts.Item(0)("DistrictName", DataRowVersion.Original)) SetRowOriginalItemValue(d.Districts.DistrictIdColumn, 109901, d.Districts.DistrictNameColumn, "Northeast")

    A 1 Reply Last reply
    0
    • N NewSilence

      Is there any way to change the original value in the DataRow? DataRow.Item(0) may have modified/current value and its original value. I need to change its original value like row.Item("ColumnName", DataRowVersion.Original) = "NewValue". But this property is for read only any. Ideas? I have found the way but I don't like it and it looks like a hack. Here's the code: Public Function SetRowOriginalItemValue(ByVal primaryKeyColumn As DataColumn, ByVal key As Object, ByVal editColumn As DataColumn, ByVal value As Object) As Boolean 'Utils.Assert.ArgumentCheckForNullReference(primaryKeyColumn, "primaryKeyColumn") 'Utils.Assert.ArgumentCheckForNullReference(key, "key") 'Utils.Assert.ArgumentCheckForNullReference(editColumn, "editColumn") 'Utils.Assert.ArgumentCheckExpectedType(key, primaryKeyColumn.DataType) 'Utils.Assert.ArgumentCheckExpectedType(value, editColumn.DataType) Dim t As DataTable = editColumn.Table Dim d As DataSet = t.DataSet Dim s As New IO.MemoryStream d.WriteXml(s, XmlWriteMode.DiffGram) s.Seek(0, IO.SeekOrigin.Begin) Dim doc As New Xml.XmlDocument doc.Load(s) Dim nsmgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(doc.NameTable) nsmgr.AddNamespace("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1") nsmgr.AddNamespace("my", d.Namespace) Dim xp As String = String.Format(New Globalization.CultureInfo("en-US"), _ "/diffgr:diffgram/diffgr:before/my:{0}[starts-with(@diffgr:id, '{0}')]/my:{1}[text()='{2}']/../my:{3}/node()", _ t.TableName, primaryKeyColumn.ColumnName, key, editColumn.ColumnName) Dim n As Xml.XmlNode = doc.DocumentElement.SelectSingleNode(xp, nsmgr) If Not n Is Nothing Then n.Value = value d.Clear() d.ReadXml(New IO.StringReader(doc.InnerXml), XmlReadMode.DiffGram) Return True End If Return False End Function '''''''''''''''''''' Sub SetRowOriginalItemValueTest() Dim d As New DistrictSet Dim row As DistrictSet.DistrictsRow row = d.Districts.NewDistrictsRow row.DistrictId = 109901 row.DistrictName = "Orange County" d.Districts.AddDistrictsRow(row) d.AcceptChanges() d.Districts(0).DistrictName = "West Coast" Diagnostics.Trace.WriteLine("Current: " & d.Districts.Item(0)("DistrictName")) Diagnostics.Trace.WriteLine("Original before SetRowOriginalItemValue(): " & d.Districts.Item(0)("DistrictName", DataRowVersion.Original)) SetRowOriginalItemValue(d.Districts.DistrictIdColumn, 109901, d.Districts.DistrictNameColumn, "Northeast")

      A Offline
      A Offline
      ApurvaShah
      wrote on last edited by
      #2

      Apply DataTable.Rows[0].AcceptChanges()

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups