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. C#
  4. How to swap primary key values in a DataTable?

How to swap primary key values in a DataTable?

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 3 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.
  • R Offline
    R Offline
    Radoslav Bielik
    wrote on last edited by
    #1

    Hello, Is it possible to easily swap values of primary key column between two rows of one DataTable object? I've been trying it like this; DataRow dr1=dt.Rows.Find(5); //dt being a DataTable instance DataRow dr2=dt.Rows.Find(10); dr1.BeginEdit(); dr2.BeginEdit(); dr1["item_Id"]=10; dr2["item_Id"]=5; dr1.EndEdit(); //this will throw an exception,                //because dr2's PK still has a value of 10 dr2.EndEdit(); Same thing happens with dt.AcceptChanges(); Is there a way how to achieve this? Any clues are highly appreciated! Rado

    T S 2 Replies Last reply
    0
    • R Radoslav Bielik

      Hello, Is it possible to easily swap values of primary key column between two rows of one DataTable object? I've been trying it like this; DataRow dr1=dt.Rows.Find(5); //dt being a DataTable instance DataRow dr2=dt.Rows.Find(10); dr1.BeginEdit(); dr2.BeginEdit(); dr1["item_Id"]=10; dr2["item_Id"]=5; dr1.EndEdit(); //this will throw an exception,                //because dr2's PK still has a value of 10 dr2.EndEdit(); Same thing happens with dt.AcceptChanges(); Is there a way how to achieve this? Any clues are highly appreciated! Rado

      T Offline
      T Offline
      Tom Archer
      wrote on last edited by
      #2

      DataRow dr1=dt.Rows.Find(5); //dt being a DataTable instance
      DataRow dr2=dt.Rows.Find(10);

      dr1.BeginEdit();
      dr1["item_Id"]=-1; // or any unused value
      dr1.EndEdit();

      dr2.BeginEdit();
      dr2["item_Id"]=5;
      dr2.EndEdit();

      dr1.BeginEdit();
      dr1["item_Id"]=10;
      dr2.EndEdit();

      dt.AcceptChanges();

      Cheers, Tom Archer Inside C#,
      Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

      R 1 Reply Last reply
      0
      • T Tom Archer

        DataRow dr1=dt.Rows.Find(5); //dt being a DataTable instance
        DataRow dr2=dt.Rows.Find(10);

        dr1.BeginEdit();
        dr1["item_Id"]=-1; // or any unused value
        dr1.EndEdit();

        dr2.BeginEdit();
        dr2["item_Id"]=5;
        dr2.EndEdit();

        dr1.BeginEdit();
        dr1["item_Id"]=10;
        dr2.EndEdit();

        dt.AcceptChanges();

        Cheers, Tom Archer Inside C#,
        Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

        R Offline
        R Offline
        Radoslav Bielik
        wrote on last edited by
        #3

        Oh thanks a lot! How come this didn't come to my mind :) Rado

        T 2 Replies Last reply
        0
        • R Radoslav Bielik

          Oh thanks a lot! How come this didn't come to my mind :) Rado

          T Offline
          T Offline
          Tom Archer
          wrote on last edited by
          #4

          Because it's common sometimes to overlook the easy stuff in search of the more complex solution. We all do it ;) Cheers, Tom Archer Inside C#,
          Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

          1 Reply Last reply
          0
          • R Radoslav Bielik

            Hello, Is it possible to easily swap values of primary key column between two rows of one DataTable object? I've been trying it like this; DataRow dr1=dt.Rows.Find(5); //dt being a DataTable instance DataRow dr2=dt.Rows.Find(10); dr1.BeginEdit(); dr2.BeginEdit(); dr1["item_Id"]=10; dr2["item_Id"]=5; dr1.EndEdit(); //this will throw an exception,                //because dr2's PK still has a value of 10 dr2.EndEdit(); Same thing happens with dt.AcceptChanges(); Is there a way how to achieve this? Any clues are highly appreciated! Rado

            S Offline
            S Offline
            StealthyMark
            wrote on last edited by
            #5

            Before editing your DataRows, set DataSet.EnforceConstraints to false.

            try
            {
            dt.DataSet.EnforceConstraints = false;
            // Edit your Rows
            // EndEdit
            }
            finally
            {
            // If you violated any constraints, like PKs,
            // this statement will throw an exception.
            dt.DataSet.EnforceConstraints = true;
            }

            R 1 Reply Last reply
            0
            • S StealthyMark

              Before editing your DataRows, set DataSet.EnforceConstraints to false.

              try
              {
              dt.DataSet.EnforceConstraints = false;
              // Edit your Rows
              // EndEdit
              }
              finally
              {
              // If you violated any constraints, like PKs,
              // this statement will throw an exception.
              dt.DataSet.EnforceConstraints = true;
              }

              R Offline
              R Offline
              Radoslav Bielik
              wrote on last edited by
              #6

              Thanks for the tip! Rado

              1 Reply Last reply
              0
              • R Radoslav Bielik

                Oh thanks a lot! How come this didn't come to my mind :) Rado

                T Offline
                T Offline
                Tom Archer
                wrote on last edited by
                #7

                (Repeated from our private email so that others searching for this answer will find it.) Hi Rado, Ever have one of those problem that just eats at you? Well your situation has bugged me for some time because I just knew there had to be an easier, more elegant way. There is...... The problem was that BeginEdit calls for the two DataRow objects were disabling the constraints for the respective rows, but once you called EndEdit for one DataRow, the primary key value for that first row conflicted with the (not yet committed) second DataRow object. Therefore, I had to assume that there was a higher level way of turning off constraints - either the DataRowCollection or the DataTable. Finally, I found the DataTable::BeginLoadData and DataTable::EndLoadData method, which turn off and on, respectively, constraint checking and index maintenance for the entire table. table.BeginLoadData(); // switch the primary keys for your two DataRow objects table.EndLoadData(); Note: You mentioned the fact that someone suggested turning constraints off for the entire DataSet. As this technicall will work, using BeginLoadData and EndLoadData will affect only one table instead of all the tables for a given DataSet. Cheers, Tom Archer Inside C#,
                Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

                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