Weird problem while inserting a record
-
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"]); }
-
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"]); }
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.
-
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.
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).