Hi there,
I am trying to find a good way to modify some values in the row I am working on.
The situation is as follows:
I have a grid with a startdate, enddate and duration. On doubleclicking and editting for example the startdate the BeforeCellUpdate is called when leaving the cell.
In the BeforeCellUpdate there are a few checks to see if there are overlaps, etc. and finally I want to set the cells on the selected row so I do the following:
grid.ActiveCell.Row.Cells["ColumnName"].Value = e.NewValue;
Now when editting the cell I get the following exception on this row:
InvalidOperationException
Value property can not be assigned while BeforeCellUpdate event is in progress
I have been thinking of globally setting a variable and updating the values in the AfterCellUpdate but isn't there a better way to resolve this?
Thank in advance!
Hi,
It's a little odd to update other cells in BeforeCellUpdate, because the update in the current cell hasn't completed, yet. I recommend using AfterCellUpdate, which fires after the current update has been committed to the underlying data source.
Having said that, I'm not sure why the grid is not allowing you to update the Value of other cells. I can understand why it doesn't let you change the Value of the cell currently in the process of being updated, but I don't see why other cells would cause a problem. I might just be missing something, so I will check it out and get back to.
But in any case, I still think it makes more sense to use AfterCellUpdate because the update of the current cell could fail and then you will have updated the other cells in the row erroneously.
Thanks for your response!
The reason of using the BeforeCellUpdate for this is because I want to use the old AND the new value to calculate another cell in the same row.
I am having 2 DateTime values. Now when I modify the start date I want to update the end date with the following calculation:
TimeSpan difference = e.NewValue - e.Cell.Value
DateTime oldEndDate = grid.Rows[0].Cells[enddate].Value
DateTime newEndDate = oldEndDate += difference
Something like that. Now I also tried using the:
grid.Selected.Rows[0].Cells[enddate].Value = newEndDate
But I get an IndexOutOfBounds exception because the grid.Selected.Rows.Count = 0
Accessing the row through the ActiveCell also gives me an exception.