Saturday, August 8, 2009

Things That Drive Me Crazy

Things That Drive Me Crazy

In a C# DataTable, you can read values in a DataRow through ItemArray, like this:

for (int i = 0; i < dr.ItemArray.Length; i++)
Debug.WriteLine(dr.ItemArray[i]);

But heaven help you if you try to update a DataRow through ItemArray[i]. It doesn't work. I spent a lot of time trying to figure out why, because I had some code working in a somewhat ugly way, using:

dt.Rows[matchRowNbr][i] = newValue;

This is ugly, because it is more elegant to process one DataRow at a time. In the process of revising this code, the above turned into:

dr.ItemArray[i] = newValue;

And then it no longer worked. You can read through ItemArray, but you can't write back to the DataRow that way. The technical discussion of why is here.

No comments:

Post a Comment