Crono
Crono

Reputation: 10488

Confused on DataSet.Merge and RowState

Consider the following, oversimplified DataTable object:

enter image description here

Id is an autoincrementing integer that acts at the table's primary key, while Text is a simple String typed column.

Now consider the following code:

private static void Main(string[] args)
{
    var dataSet = new TestingDataSet();

    var row = dataSet.TestingTable.AddTestingTableRow("First");

    dataSet.AcceptChanges();

    var copiedDataSet = dataSet.Copy();

    Console.WriteLine("Row after initialization:");
    Console.WriteLine();

    Console.WriteLine("Text : " + row.Text);
    Console.WriteLine("State: " + row.RowState.ToString());
    Console.WriteLine();

    row.Text = "Modifications";

    Console.WriteLine("Row after changes:");
    Console.WriteLine();

    Console.WriteLine("Text : " + row.Text);
    Console.WriteLine("State: " + row.RowState.ToString());
    Console.WriteLine();

    dataSet.Merge(copiedDataSet, false);

    Console.WriteLine("Row after merge:");
    Console.WriteLine();

    Console.WriteLine("Text : " + row.Text);
    Console.WriteLine("State: " + row.RowState.ToString());
    Console.WriteLine();

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

That code produces the following result:

enter image description here

Look at the final RowState value. It is modified. How come? The copied dataset instance had no changes, and I explicitely set the preserveChanges parameter to false. Therefore shouldn't the row now be considered Unchanged?

This MSDN article states that when rows are merged, their state follows as well. Is the article wrong, or is something in my code is?

Upvotes: 2

Views: 1356

Answers (2)

ReLiesDev
ReLiesDev

Reputation: 33

I ran into this same issue, and ended up getting around it by pulling all changes out of the main DataTable object, running AcceptChanges on the core object, then doing work on the individual tables and merging them back into the main. That way everything in the core DataTable object is "Unmodified", and will just absorb the RowState of the merged data without any unexpected adjustments like this.

Upvotes: 0

Crono
Crono

Reputation: 10488

So I dug a bit deeper in the documentation and came across this:

If the incoming row has a RowState of Unchanged and the existing row has a RowState of Modified, Deleted, or Added, the RowState of the existing row is set to Modified.

So it seems that this is the expected behavior when merging unchanged rows into modified rows.

Full article can be found here.

Upvotes: 1

Related Questions