David David - 1 year ago 54
Vb.net Question

Clear Text from UltraGridCell when Data is not Valid

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.

What I have so far 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 = ""
End If
End If
Catch
End Try


The
MessageBox
line works fine, however, another one is then displayed saying


"Unable to convert from: System.String to System.Decimal"


The cell is a decimal cell, because it's for entering a monetary value, so how do I set the value to "" for
Decimal
?

Answer Source

The value you are getting from the database when no value is provided is DBNull.Value. So if you need the cell to show nothing you need to set same value like this:

e.Cell.Value = DBNull.Value

This will force the grid to show an empty cell

[Edit] Use this code in BeforeEnterEditMode event to suppress user from editing a cell when the related check box cell is not checked:

    Private Sub UltraGrid1_BeforeEnterEditMode(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles UltraGrid1.BeforeEnterEditMode
    Dim cell As UltraGridCell = Me.UltraGrid1.ActiveCell
    If cell Is Nothing Then
        Return
    End If
    Try
        If cell.Column.Key = "Commission_Rate" Then
            If 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.Cancel = True
            End If
        End If
    Catch
    End Try
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download