I have an UltraGrid which is bound to a data source. One of the columns has a date field in it and the other columns have checkboxes. When I change the selected date this will affect which checkboxes are checked. Simple I thought!
I have implemented a gridResults_CellChange handler and identfiied when the date cell changes. I then get the UltraDataRow which is attached to the grid row
var udr = (UltraDataRow)e.Cell.Row.ListObject;
...and then update one of the checkbox cells.
udr[dayStartCellIndex] = false;
...this causes the gridResults_InitialiseRow event to fire so that I can re-display the row as required. However when I look at the same cell value it has not been set to the value set above. It has reverted to the original value (which was in fact System.DBNull and I hide some checkboxes if there valus is this).
What am I missing? It seems logical to update the underlying UltraDataRow when i edit a cell value...
I think it might help you to understand when these events fire and when the underlying data source is updated. In order to do that, it helps to think about a date cell. So suppose you have a DateTime field in your grid (as you actually do).
If the user enters that cell and begins to type in a date, the CellChange event will fire on every keystroke. So in this example, suppose the user intends to enter "12/25/2019". The first character the user types is the number "1". So the CellChange even of the grid will fire at this point, and the Text property of the cell will return "1". But the Value property of the cell reads from the underlying data source. And in such a case, the underlying data source is of type DateTime. So the string or number "1" cannot be pushed into that field, since "1" is not a valid date.
So.. any time you are working with the CellChange event, you are working with the on-screen text that the user typed in and you cannot work with the underlying cell's value - unless you want the old value that was in there prior to the user making a change. You can't get the NEW value of the cell until the user leaves that cell or otherwise commits the change by losing focus on the grid or if the grid.UpdateData method is called.
So what you could do here is ignore CellChange entirely, and simply update the checkboxes in the InitializeRow event. This event is specifically designed for situations like this and it fires any time a value in a cell is committed or otherwise changed in the underlying data source.
The down side of that approach is that if the user selects a date from the dropdown and types in a date, the checkboxes will not updated until that change is committed. And that only happens (as I mentioned) when user either leaves the cell, leaves the grid, or the UpdateData method is called.
So if you want to update the checkboxes as the user is in the process of making changes to the cell, you would have to handle the CellChange event, get the text, and then try to convert that text into a valid date. You could use DateTime.TryParse, for example. Then you could get a valid date based on what is on the screen and adjust the CheckBox cells as you like. The tricky part of this, is that you will need to decide what to do in the case where the text in the cell is not a valid date.
Should you have further questions, please let me know.
Sincerely,Tihomir TonevAssociate Software Developer
UltraGridOnCellUpdate.zip