I need to activate the current cell in the _AfterCellUpdate event after the error message is shown to tell the user that the value of the cell is wrong. But the next cell is activated when the tab is pressed.
The code below shows that I am doing everything to keep the focus on the same cell, but the focus is moved to the next cell!
void ultraGridUM_AfterCellUpdate(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e) { if (e.Cell.Column.Key == "Code" && e.Cell.Value!=null) { foreach (Infragistics.Win.UltraWinGrid.UltraGridRow row in this.ultraGridUM.Rows) { if (row != e.Cell.Row) { if (row.Cells["Code"].Value != DBNull.Value) { if (String.Compare(row.Cells["Code"].Value.ToString(), e.Cell.Value.ToString().Trim()) == 0) { MessageBox.Show("Wrong value."); e.Cell.Value = e.Cell.OriginalValue; this.ultraGridUM.ActiveRow = e.Cell.Row; this.ultraGridUM.ActiveCell = e.Cell; this.ultraGridUM.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode,false,false); return; } } } } } }
The best thing to do would be to validate the cell before it it updated, instead of after. Use BeforeCellUpdate and you can cancel the operation and keep the focus in the cell, instead of trying to put it back in after the user already left.
I fount out that e.Cancel will cancel the cell update, but how can I activate that cell? It seems that the cell that I click will activate or the next cell if I use the Tab. Also is there a way to set the last value back?
You can't activate the cell inside BeforeCellUpdate, because the cell is already active. Activating the ActiveCell will not do anything.
What you probably need to do is use BeforeExitEditMode and cancel that event to keep the cell from leaving edit mode in the first place.
Thanks! Done!