Update Cell Value in CellValidating Event

3 posts, 0 answers
  1. Stephen
    Stephen avatar
    2 posts
    Member since:
    Mar 2017

    Posted 09 May 2017 Link to this post

    We have a simple datagrid where the first column contains a Vendor Number and the second is a read-only column containing the name of that vendor.  Users can freely enter into the first column, and in the CellValidation event, we verify that the format of what they entered is correct and then lookup the number they entered to populate the second column with the name.

    The function looks something like this:

    Private Sub ValidateCell(sender As System.Object, e As Telerik.WinControls.UI.CellValidatingEventArgs)
        Dim vendNum As String = ""
        If e.ColumnIndex = 0 AndAlso Not String.IsNullOrWhiteSpace(e.Value) Then
            vendNum = e.Value
             
            'Format the Vendor Number
            vendNum = FormatVendorNumber(vendNum)
            Me.dgvVendors.CurrentCell.Value = vendNum 'dgvVendors is our data grid
     
            'Find the name of the vendor
            e.Row.Cells(1).Value = GetVendorName(vendNum)
        End If
    End Sub

     

    If a cell enters edit mode and the value is changed, then the value displayed in the cell is not updated with the formatted version.  However, if a cell enters edit mode and then leaves edit mode with no changes, the formatted value from our function is applied.  For example, if you were to add a new row and type "1" in the cell, our format function would convert that to "00001.00", and if you put a breakpoint in the above code, you would see that both Me.dgvVendors.CurrentCell.Value and the vendNum variable would reflect this formatted value.  However, once processing completes, the cell will still read "1" in the UI.

    Now, after having done that, if you click the same cell to enter edit mode and then click somewhere else, the same function above will run, only this time the cell in the UI will be updated to display "00001.00" properly.  Likewise, if you edit that formatted value and remove a zero, the function will fail to update the value, but if after doing that, you click on the cell and then leave it without making any changes, it will be formatted again and the missing zero will be added back.  The validating event handler can only update the cell value if that value was not actually changed during editing.

     

    Note: The last line setting e.Row.Cells(1).Value to the Vendor Name always works.  We've tried this syntax to apply our updated Vendor Number to Cells(0), but get the exact same results as using Me.dgvVendors.CurrentCell.Value.

    Any idea what's going on here or how to correct it?

  2. Stephen
    Stephen avatar
    2 posts
    Member since:
    Mar 2017

    Posted 09 May 2017 in reply to Stephen Link to this post

    I changed our structure somewhat so that the formatting now occurs in the CellEndEdit event instead and that seems to work, but it doesn't explain why the above code worked when the cell's value wasn't actually changed during Edit but it failed when the value was changed.
  3. Hristo
    Admin
    Hristo avatar
    1510 posts

    Posted 10 May 2017 Link to this post

    Hi Stephen,

    Thank you for writing.

    I am glad that you have found a working solution for this scenario. The Validating event is usually used with the Cancel flag if the validation does not pass. When you set the e.Cancel parameter to true the cell will remain in edit mode allowing your end users to enter the correct value. In your actual setup indeed it is more appropriate to use the CellEndEdit event because you would want to change the actual cell`s value.

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top