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. Database & SysAdmin
  3. Database
  4. Weird problem while inserting a record

Weird problem while inserting a record

Scheduled Pinned Locked Moved Database
help
3 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.
  • 2 Offline
    2 Offline
    2sky
    wrote on last edited by
    #1

    Hello, I wonder if anyone can help me with this. I try to insert a record in a DataTable, and everything works absolutely perfect. UNTIL I create a custom DataView object based on the table. Here is a small sample, when you take the DataView myView = new DataView(table); line out of comment the record isn't at the correct position anymore. Any help is much appreciated !!!! DataTable table = new DataTable("Table"); DataColumn colID = table.Columns.Add("ID", typeof(int)); DataColumn colName = table.Columns.Add("Name", typeof(String)); colID.AllowDBNull = false; table.Rows.Add(new object[] {1, "Row 1"}); table.Rows.Add(new object[] {2, "Row 2"}); table.Rows.Add(new object[] {4, "Row 4"}); table.AcceptChanges(); //DataView myView = new DataView(table); foreach(DataRowView row in table.DefaultView) { Console.WriteLine(row["Name"]); } Console.WriteLine("-------------------------------------------------------"); DataRowView cindyView = table.DefaultView.AddNew(); cindyView["ID"] = 3; cindyView["Name"] = "Row 3"; Object[] ItemArray = cindyView.Row.ItemArray; cindyView.CancelEdit(); DataRow cindy = table.NewRow(); cindy.ItemArray = ItemArray; cindy.EndEdit(); table.Rows.InsertAt(cindy, 2); table.DefaultView.RowStateFilter = DataViewRowState.None; table.DefaultView.RowStateFilter = DataViewRowState.CurrentRows; foreach(DataRowView row in table.DefaultView) { Console.WriteLine(row["Name"]); }

    M 1 Reply Last reply
    0
    • 2 2sky

      Hello, I wonder if anyone can help me with this. I try to insert a record in a DataTable, and everything works absolutely perfect. UNTIL I create a custom DataView object based on the table. Here is a small sample, when you take the DataView myView = new DataView(table); line out of comment the record isn't at the correct position anymore. Any help is much appreciated !!!! DataTable table = new DataTable("Table"); DataColumn colID = table.Columns.Add("ID", typeof(int)); DataColumn colName = table.Columns.Add("Name", typeof(String)); colID.AllowDBNull = false; table.Rows.Add(new object[] {1, "Row 1"}); table.Rows.Add(new object[] {2, "Row 2"}); table.Rows.Add(new object[] {4, "Row 4"}); table.AcceptChanges(); //DataView myView = new DataView(table); foreach(DataRowView row in table.DefaultView) { Console.WriteLine(row["Name"]); } Console.WriteLine("-------------------------------------------------------"); DataRowView cindyView = table.DefaultView.AddNew(); cindyView["ID"] = 3; cindyView["Name"] = "Row 3"; Object[] ItemArray = cindyView.Row.ItemArray; cindyView.CancelEdit(); DataRow cindy = table.NewRow(); cindy.ItemArray = ItemArray; cindy.EndEdit(); table.Rows.InsertAt(cindy, 2); table.DefaultView.RowStateFilter = DataViewRowState.None; table.DefaultView.RowStateFilter = DataViewRowState.CurrentRows; foreach(DataRowView row in table.DefaultView) { Console.WriteLine(row["Name"]); }

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      It would help if you posted the actual output from this code and what you expected to see. IIRC, unless you've applied a primary key constraint, or some kind of sort criteria, your view will show the records in the order they were inserted.

      2 1 Reply Last reply
      0
      • M Mike Dimmick

        It would help if you posted the actual output from this code and what you expected to see. IIRC, unless you've applied a primary key constraint, or some kind of sort criteria, your view will show the records in the order they were inserted.

        2 Offline
        2 Offline
        2sky
        wrote on last edited by
        #3

        I get the following output if I don't create the additional DataView: Row 1 Row 2 Row 4 ----- Row 1 Row 2 Row 3 Row 4 And this is the output when the DataView object is created based on the table: Row 1 Row 2 Row 4 ----- Row 1 Row 2 Row 4 Row 3 I don't see, why the DevaultView of the table changes the order of the DataViewRows, just because I create another DataView object based on this DataTable. Also, when I go in debug mode and check the Rows collection of the DataTable, the rows appear as one would expect (1, 2, 3, 4).

        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