I'm trying to add some validation into my UltraGrid, whereby, if the user types a value into one cell, but has not set the value of a CheckBoxColumn to True, a MessageBox gets displayed and the text/value in that cell is cleared, leaving a blank cell again.
UltraGrid
CheckBoxColumn
True
MessageBox
What I have so far in the CellChange method is:
Try If e.Cell.Column.Key = "Commission_Rate" Then If e.Cell.Row.Cells("Commission_Override").Value = False Then MessageBox.Show("Before entering a custom rate, please set 'Commission Override' to True", "Override Commission", MessageBoxButtons.OK) e.Cell.Value = DBNull.Value End If End If Catch End Try
However, that doesn't update the value until closing the MessageBox, and then setting the CheckBoxColumn to True. Only when that has been done will it set the value to ""/clear the entered text.
My guess is that is's something to do with the value and text being different, but I may be wrong.
How do I modify the code to set the value to nothing/null without having to change another cell value first?
Hello David,
If you need to validate the user input you may handle BeforeEnterEditMode event of the grid. In this event you can check if the corresponding check box is checked. If not cancel the event and the cell will not enter in edit mode. This will suppress the user to enter any input in the cell and the cell will retain its initial value, whatever it was. You can use code like this:
Private Sub UltraGrid1_BeforeEnterEditMode(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles UltraGrid1.BeforeEnterEditMode Dim activeCell As UltraGridCell = Me.UltraGrid1.ActiveCell If activeCell Is Nothing Then Return End If Try If activeCell.Column.Key = "Commission_Rate" Then If activeCell.Row.Cells("Commission_Override").Value = False Then MessageBox.Show("Before entering a custom rate, please set 'Commission Override' to True", "Override Commission", MessageBoxButtons.OK) ' Cancel the event to disallow cell to enter edit mode. This will prevent user to type any value in the cell ' and will retain the cell's original value e.Cancel = True End If End If Catch End TryEnd Sub
Attached is a small sample showing this approach. Please, check my sample and let us know if any additional questions arise.