Reputation: 1146
I have DataRow from grid and I need to modify few columns in one row. So I put all my columns in Array, and tried to modify them but it doesn't work as I wish. I need an explanation for that.
My goal is to get all columns in specific order in Array or some collection, and then modify them etc. I think I am now creating some new objects which reference to something else than my column. Maybe I should try to store in collection some references? Using ref should is best option?
DataRow dr = rows[i] as DataRow;
dr["size"] = 5000; // => size is 5000;
ChangeSize(dr); // => size is 6000;
private void ChangeSize(DataRow dataRow)
{
dataRow["size"] = 6000; // => size is 6000
Object[] arrayOfColumns= { dataRow["size"], ... };
arrayOfColumns[0] = 7000; // => won't change size...
}
Upvotes: 0
Views: 94
Reputation: 2700
dataRow["size"] contains an int which is a value type.
When you instanciate and intialize arrayOfColumns you get a copy of the value contained at dataRow["size"] and not a reference.
Upvotes: 1
Reputation: 1503859
You're just changing the value in the array. You happened to initialize that via dataRow["size"]
but that doesn't mean there's any perpetual link between the two.
If you need changes to be reflected back to the DataRow
, I suspect you should have another method to do that:
private void CopyToDataRow(Object[] source, DataRow target)
{
target["size"] = source[0];
// etc
}
There's no concept of triggering custom code like this whenever an array is modified - you'll need to call it at the appropriate time. (And no, ref
wouldn't help you here at all.)
Upvotes: 1