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.
There are a couple of things you could try.
1) Instead of performing the update in BeforeCellUpdate, simply store the old value in BeforeCellUpdate and then use that value to perform the operation you need inside of AfterCellUpdate.
2) Another approach would be to take the code you have in BeforeCellUpdate and move it into another method, then call that code via a BeginInvoke. This will cause the method to be called after the event has completed and the grid is finished with all of it's processing. This is a little bit risky and you could run into some timing issue, so I think approach #1 is more reliable.
Daan Vermeulen said: 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
Now I also tried using the:
Using Selected will never work. If there is a cell currently in edit mode, then there can never be a Selected cell at the same time. Selection of rows, cells, and columns is removed as soon as you try to enter edit mode on any cell.
ActiveCell will return the cell being edited, but I don't think that will do you any good. You already have that cell via e.Cell. It's the same object regardless of how you access it.
I selected the first of the 2 solutions. When I will have some more time I will also try the second solution sometime.
Thanks for your time!