0

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;

ChangeSize body

private void ChangeSize(DataRow dataRow)
{
             dataRow["size"] = 6000; // => size is 6000
             Object[] arrayOfColumns= { dataRow["size"], ... };
             arrayOfColumns[0] = 7000; // => won't change size...

}
4

2 回答 2

1

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.)

于 2013-10-08T07:54:55.623 回答
1

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.

于 2013-10-08T07:56:07.790 回答