OK, this is going to get interesting, because it's kinda fundamental to how .NET works.
DataRow is a Class - which means it's what's called a "Reference type" rather than a "Value type" as (for example) integers are.
When you create an integer variable and assign it a value:
int x = 5;
int y = x;
You crate a new variable and copy the value into it: so if you then do this:
x = x + 10;
Console.WriteLine("{0}:{1}", x, y);
It will print:
15:5
Because
y
got a copy of the content of
x
, not a reference to it. This is what you expect!
Reference types are different:
public class MyClass
{
public int Val;
}
...
MyClass x = new MyClass();
x.Val = 5;
MyClass y = x;
x.Val = x.Val + 10;
Console.WriteLine("{0}:{1}", x.Val, y.Val)
This time it prints
15,15
Because
y
got a copy of the content of
x
, which was a reference to the actual instance. So when you changed the Val via the x variable, it also changed the Val of the instance that y referred to because they are both referencing the same instance!
The same thing is happening in your code:
DataRow dr = mydataset.Tables["NewTable"].Rows[0];
Createa a new variable called
dr
, and assigns a copy of the reference to the Table row to it - they both refer to the same area of memory, the same instance of a Row.
So then:
dr["Name"] = TextBox2.Text;
Changes the one and only instance - which affects the instance in the table because it's the same one!
Does that make sense?
[edit]
There is a more detailed description of the difference here:
Using struct and class - what's that all about?[
^]
[/edit]