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. DataRow, DataTable, DataGridView, avoid changing values in the DataRow

DataRow, DataTable, DataGridView, avoid changing values in the DataRow

Scheduled Pinned Locked Moved C#
csharphelp
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.
  • E Offline
    E Offline
    Elvia Gonzalez
    wrote on last edited by
    #1

    Hi everybody! I have the next code and I’m having a problem with the DataRow: DataTable dt = new DataTable(); dt.Load(GetReader()); dataGridView.DataSource = dt; DataRow[]originalRows = dt.Select(); The problem is that when I change a cell value in the datagridview the change is also set in the originalRows values. I’d like to avoid this, since I want to use the original values of the datatable to compare them later to see whether there ware changes in my datagridview. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005

    E L J 3 Replies Last reply
    0
    • E Elvia Gonzalez

      Hi everybody! I have the next code and I’m having a problem with the DataRow: DataTable dt = new DataTable(); dt.Load(GetReader()); dataGridView.DataSource = dt; DataRow[]originalRows = dt.Select(); The problem is that when I change a cell value in the datagridview the change is also set in the originalRows values. I’d like to avoid this, since I want to use the original values of the datatable to compare them later to see whether there ware changes in my datagridview. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005

      E Offline
      E Offline
      Elvia Gonzalez
      wrote on last edited by
      #2

      I forgot to say that I tried the following but it didn't work: private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { originalRows[e.RowIndex].CancelEdit(); originalRows[e.RowIndex].EndEdit(); }

      1 Reply Last reply
      0
      • E Elvia Gonzalez

        Hi everybody! I have the next code and I’m having a problem with the DataRow: DataTable dt = new DataTable(); dt.Load(GetReader()); dataGridView.DataSource = dt; DataRow[]originalRows = dt.Select(); The problem is that when I change a cell value in the datagridview the change is also set in the originalRows values. I’d like to avoid this, since I want to use the original values of the datatable to compare them later to see whether there ware changes in my datagridview. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Look into using DataSet rather than DataTable directly for loading and as DataSource


        "What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum led mike

        1 Reply Last reply
        0
        • E Elvia Gonzalez

          Hi everybody! I have the next code and I’m having a problem with the DataRow: DataTable dt = new DataTable(); dt.Load(GetReader()); dataGridView.DataSource = dt; DataRow[]originalRows = dt.Select(); The problem is that when I change a cell value in the datagridview the change is also set in the originalRows values. I’d like to avoid this, since I want to use the original values of the datatable to compare them later to see whether there ware changes in my datagridview. Thanks in advance, Elvia PS: Windows application in Microsoft Visual C# 2005

          J Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #4

          You're trying to solve this problem the wrong way. The Select method of the DataTable returns references to the rows in the table, not clones. When a value in a row is changed via the grid, the corresponding row in the table (and, hence, an element in your originalRows array) will reflect that change. If all that you need to do is detect if values in the table have been modified, you can loop over every row in the table and check the RowState property. Josh

          E 1 Reply Last reply
          0
          • J Josh Smith

            You're trying to solve this problem the wrong way. The Select method of the DataTable returns references to the rows in the table, not clones. When a value in a row is changed via the grid, the corresponding row in the table (and, hence, an element in your originalRows array) will reflect that change. If all that you need to do is detect if values in the table have been modified, you can loop over every row in the table and check the RowState property. Josh

            E Offline
            E Offline
            Elvia Gonzalez
            wrote on last edited by
            #5

            Thanks Led and Josh, I want to compare an specific cell not the whole row. I have the following comparation. if (!originalRows[i]["Path"].Equals(testCase["Path"])) Do you know how can I compare the cell's state? I'll try the DataSet later and I'll tell you the results. Elvia

            J 1 Reply Last reply
            0
            • E Elvia Gonzalez

              Thanks Led and Josh, I want to compare an specific cell not the whole row. I have the following comparation. if (!originalRows[i]["Path"].Equals(testCase["Path"])) Do you know how can I compare the cell's state? I'll try the DataSet later and I'll tell you the results. Elvia

              J Offline
              J Offline
              Josh Smith
              wrote on last edited by
              #6

              You can call Copy on the DataTable, instead of Select, to get a complete and separate copy of the entire table. Later, when you want to compare each value in each row, you can just compare the copied (unmodified) table to the one bound to the grid. Josh -- modified at 15:04 Friday 5th May, 2006 You might also be able to use the overload of the Select method which takes the DataViewRowState parameter. It allows you to get the original values for modified rows.

              E 1 Reply Last reply
              0
              • J Josh Smith

                You can call Copy on the DataTable, instead of Select, to get a complete and separate copy of the entire table. Later, when you want to compare each value in each row, you can just compare the copied (unmodified) table to the one bound to the grid. Josh -- modified at 15:04 Friday 5th May, 2006 You might also be able to use the overload of the Select method which takes the DataViewRowState parameter. It allows you to get the original values for modified rows.

                E Offline
                E Offline
                Elvia Gonzalez
                wrote on last edited by
                #7

                Hi Josh! Thanks a lot. I called the Copy method and did what you told me. It works as I want. I appreciate it :) Elvia

                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