1

Possible Duplicate:
Unable to change DataRow value

I bound a datatable to a datagrid. Now I would like to change the datatable's value corresponding to the row of the selected cell once a button is clicked. Here is my code:

    private void BtnModifyColorBlue_Click(object sender, RoutedEventArgs e)
    {
        if (dataGrid.SelectedCells.Count < 1)
            return;
        DataGridCellInfo dc = dataGrid.SelectedCells.FirstOrDefault();
        DataRowView drv = dc.Item as DataRowView;
        if (drv == null)
            return;
        drv.Row.ItemArray[11] = Brushes.Blue;
    }                                                                                      

For some reason, after the assignment, the datatable's value is untouched. Are there any mysterious things happening here? BTW, I can conform that the assignment has been executed. Thanks a lot.

Community
  • 1
  • 1
Bob
  • 381
  • 4
  • 14
  • Sorry. I did know ItemArray was the problem. I thought the problems might come from the DataGridCellInfo. So When I search, I emphasis DataGridCellInfo part and that this why I had not found that quesetion. – Bob May 21 '12 at 13:54
  • For more context on why it does this, you can read the source code here: https://referencesource.microsoft.com/#System.Data/System/Data/DataRow.cs,386 The accessor for ItemArray creates a new array, so modifying its values doesn't affect values of the items in the DataRow itself. – Jeff B Apr 17 '19 at 14:17

1 Answers1

13

You can't change through the item array, use the syntax below:

drv.Row[11] = Brushes.Blue;
Rich
  • 3,781
  • 5
  • 34
  • 56