Reputation: 5143
The purpose is to move a row into a DataTable to the first position, so once I get the row I remove it and then I insert it into the first position.
DataRow[] dr = DataSet1.Tables[0].Select("field1 = HELLO");
DataSet2.Tables[0].Rows.Remove(dr[0]); // Here dr[0] is removed. Why?
DataSet2.Tables[0].Rows.InsertAt(dr[0], 0); // Now a null value is inserted
the second line remove both the value into de DataTable and the dr[0]
passed as parameter so I cannot insert then at the first position.
Upvotes: 0
Views: 610
Reputation: 1456
Try the below logic...
var row = DataSet2.Tables[0].Select("field1 = HELLO").First();
var oldIndex = DataSet2.Tables[0].Rows.IndexOf(row);
DataSet2.Tables[0].Rows.InsertAt(row, 0);
DataSet2.Tables[0].Rows.RemoveAt(oldIndex);
this is just logic.. so ignored null checks and exception handling
Upvotes: 0
Reputation: 2983
This is because dr[0] is just a reference, you will need to clone it and then remove it.
Upvotes: 1