3

I'm sure it's trivial but it's been a long day.

I have a DataTable in which one row has an empty cell. I want to find the row that has the empty cell, and simply assign a string to that cells value.

However when I step through the code during debug, the value never gets plugged into the table. What am I doing wrong???

currentTime = DateTime.Now.ToString();

for (int i = 0; i < OwnersTable.Rows.Count; i++)
    if (OwnersTable.Rows[i].ItemArray[9].ToString() == "")
        OwnersTable.Rows[i].ItemArray[9] = currentTime;

I found to accomplish this I had to create an entirely new row, copy every cell's contents of the existing row over, and then add it back to the table. What???
Why didn't the simple cell assignment work in the code above????

3 Answers 3

4

The getter of the DataRow.ItemArray returns an array containing entire values for the row, modifying it's elements does not make any change to the row values.

Gets or sets all the values for this row through an array.

So you need to assign an array to it instead (use the setter), but I do not recommend this way:

currentTime = DateTime.Now.ToString();

for (int i = 0; i < OwnersTable.Rows.Count; i++) {
    object[] items = OwnersTable.Rows[i].ItemArray;         
    if (items[9].ToString() == string.Empty) {            
        items[9] = currentTime
        OwnersTable.Rows[i].ItemArray = items;
    }
}

You can use the SetField method of DataRow instead:

currentTime = DateTime.Now.ToString();
foreach (DataRow row in OwnersTable.Rows) {
    string value = row.Field<string>(9);
    if (string.IsNullOrEmpty(value)) {            
        row.SetField<string>(9, currentTime) 
    }
}

Note that I assumed the field is of string type.

Sign up to request clarification or add additional context in comments.

4 Comments

Where'd you come up with these Field and SetField methods?
What do you mean by that? sorry i'm not that good in english :d
That's ok. Following your suggestion of row.Field and row.SetField I get the below errors. Error 27 'object' does not contain a definition for 'SetField' Error 25 'object' does not contain a definition for 'Field'
I updated the answer. It was because I used "var" instead of "DataRow".
0

Just came across this post after experiencing the same problem. A more direct way to do this that bypasses the ItemArray problem Mehrzad mentions is to both test and assign values using row[columnIndex] wherever row.ItemArray[columnIndex] is used. The direct indexer of the row does not reference a copy of the row's data. The code becomes:

for (int i = 0; i < OwnersTable.Rows.Count; i++)

if (OwnersTable.Rows[i][9].ToString() == "") OwnersTable.Rows[i][9] = currentTime;

Comments

0

I was facing through same issue. Apparently you don't need the key word ItemArray while assigning the value.

So instead of OwnersTable.Rows[i].ItemArray[9] = currentTime;

You could use OwnersTable.Rows[i][9] = currentTime;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.