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. Windows Forms
  4. deleted DataRows just vanishes from DataTable, no way to access them

deleted DataRows just vanishes from DataTable, no way to access them

Scheduled Pinned Locked Moved Windows Forms
question
4 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.
  • I Offline
    I Offline
    imak
    wrote on last edited by
    #1

    I have a datatable that acts as a DataSource for control. Programmatically I am also deleting some rows from this table using code like foreach (DataRow row in myDataTable.Rows) { if (row["Id"].ToString().CompareTo(myControl.SelectedValue.ToString()) == 0) { row.Delete(); break; } } I am not calling AcceptChanges on datatable. However after this delete, I don’t see any rows that have a status DataViewRowState.Deleted Any ideas what I may be missing?

    I L 2 Replies Last reply
    0
    • I imak

      I have a datatable that acts as a DataSource for control. Programmatically I am also deleting some rows from this table using code like foreach (DataRow row in myDataTable.Rows) { if (row["Id"].ToString().CompareTo(myControl.SelectedValue.ToString()) == 0) { row.Delete(); break; } } I am not calling AcceptChanges on datatable. However after this delete, I don’t see any rows that have a status DataViewRowState.Deleted Any ideas what I may be missing?

      I Offline
      I Offline
      imak
      wrote on last edited by
      #2

      I have this test code where I can see the same behavior. Can someone point what am I doing wrong here? static void Main(string[] args) { DataTable table = new DataTable(); DataColumn col = new DataColumn("ID", System.Type.GetType("System.Int32")); table.Columns.Add(col); for (int i = 0; i < 10; i++) { DataRow row = table.NewRow(); row["ID"] = i; table.Rows.Add(row); } Console.WriteLine(table.Rows.Count); for (int i = 0; i < table.Rows.Count;i++ ) { if (Convert.ToInt32(table.Rows[i]["ID"]) == 4) { table.Rows[i].Delete(); break; } } Console.WriteLine(table.Rows.Count); DataTable deletedRows = table.GetChanges(DataRowState.Deleted); // This returns NULL, shouldn't it have one row here??? for (int i = 0; i < deletedRows.Rows.Count; i++) { Console.WriteLine("Deleted: " + deletedRows.Rows[i]["ID",DataRowVersion.Original].ToString()); } }

      I 1 Reply Last reply
      0
      • I imak

        I have this test code where I can see the same behavior. Can someone point what am I doing wrong here? static void Main(string[] args) { DataTable table = new DataTable(); DataColumn col = new DataColumn("ID", System.Type.GetType("System.Int32")); table.Columns.Add(col); for (int i = 0; i < 10; i++) { DataRow row = table.NewRow(); row["ID"] = i; table.Rows.Add(row); } Console.WriteLine(table.Rows.Count); for (int i = 0; i < table.Rows.Count;i++ ) { if (Convert.ToInt32(table.Rows[i]["ID"]) == 4) { table.Rows[i].Delete(); break; } } Console.WriteLine(table.Rows.Count); DataTable deletedRows = table.GetChanges(DataRowState.Deleted); // This returns NULL, shouldn't it have one row here??? for (int i = 0; i < deletedRows.Rows.Count; i++) { Console.WriteLine("Deleted: " + deletedRows.Rows[i]["ID",DataRowVersion.Original].ToString()); } }

        I Offline
        I Offline
        imak
        wrote on last edited by
        #3

        Got it, was missing AcceptChanges()

        1 Reply Last reply
        0
        • I imak

          I have a datatable that acts as a DataSource for control. Programmatically I am also deleting some rows from this table using code like foreach (DataRow row in myDataTable.Rows) { if (row["Id"].ToString().CompareTo(myControl.SelectedValue.ToString()) == 0) { row.Delete(); break; } } I am not calling AcceptChanges on datatable. However after this delete, I don’t see any rows that have a status DataViewRowState.Deleted Any ideas what I may be missing?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          When you delete rows using DataRow.Delete() method, they're immediately deleted from the DataTable. What DataTable.AcceptChanges() does is to propagate back the changes to the original data source of the DataTable (e.g., an SQL Server database)

          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