Validation

13 posts, 0 answers
  1. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 18 Jul Link to this post

    James

    Posted 3 hours ago

    I am attempting to add validation to my grid which has 3 columns:  ID, NAME, STATUS
    My data is not bounded to an adaptor (requirement).
    I am trying to validate for the existence of name in my database on the RowsChanging event.

    On NotifyCollectionChangedAction.Add , I pass the value of NAME to my stored procedure and determine if the NAME value exists; no problems.
    On NotifyCollectionChangedAction.ItemChanging, I pass the values of ID and NAME to my stored procedure and determine if the NAME value exists.

    Problem is, the NAME value passed on ItemChanging is what is currently in the cell, not what is in the editor.  How can I get the editor value at this time?

  2. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 19 Jul Link to this post

    Hello James,

    Thank you for writing.

    The GridViewCollectionChangingEventArgs object should contain both values at this point. I have tested this with the following code and I am getting the correct results:
    Private Sub RadGridView1_RowsChanging(ByVal sender As Object, ByVal e As GridViewCollectionChangingEventArgs)
        If e.Action = NotifyCollectionChangedAction.ItemChanging Then
            Console.WriteLine(e.NewValue)
            Console.WriteLine(e.OldValue)
      
        End If
    End Sub

    Are you retrieving the value in a different way?

    I am looking forward to your reply.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 19 Jul in reply to Dimitar Link to this post

    Dimitar, thanks for your response.  I tested your code and it does indeed show the old and the new value.  However, I am noticing that if I switch between cells on the same row and modify both the cells, I am only getting the last cells modification old and new values.

    Is there a way to use this logic to be able to obtain the old and new values for each cell in the row itself?  Sort of a collection of old and new values to the entire row?

  5. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 20 Jul Link to this post

    Hello James,

    Thank you for writing back.

    We are storing the old value, only for the currently edited cell. All other cells have only one value which is the new value in this case. Here is how you can get it: 
    Private Sub RadGridView1_RowsChanging(ByVal sender As Object, ByVal e As GridViewCollectionChangingEventArgs)
        If e.Action = NotifyCollectionChangedAction.ItemChanging Then
            Console.WriteLine(e.NewValue)
            Console.WriteLine(e.OldValue)
            For Each item As GridViewCellInfo In radGridView1.CurrentRow.Cells
                If item.Value IsNot Nothing AndAlso item.ColumnInfo.Name <> e.PropertyName Then
                    Console.WriteLine("New Value: " & item.Value)
                End If
     
            Next item
     
        End If
    End Sub

    Let me know if I can assist you further.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  6. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 29 Jul in reply to Dimitar Link to this post

    I have tried many different scenarios and nothing is working exactly the way I need it to, so I am open to suggestions...
    I need an unbound Datagrid with 2 comboxbox columns, Employee (which contains Employee ID and Name) and Pay Code (which contains Pay Code ID and Pay Code Name).
    Simply requirements or so I thought...

    * For each row, both the Employee and Pay Code MUST be selected from the respective combobox columns for the row to be valid.
    * The Employee cannot be duplicated in the grid
    * When adding or editing a row, allow User to cancel entry at any time using the Escape Key, especially when Validations fails. 

    I have tried Cell Validating, RowValidating, RowChanging, and PreviewKeyDown suggestions from various threads reviewed and I still cannot mash it all together to get it exactly the way I need to.
    Any help is greatly appreciated.

  7. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 01 Aug Link to this post

    Hello James,

    I think that the best approach for this would be to use the ValueChanging event. This way you will be able to cancel the change if the employee is already added to the grid:
    Private Sub RadGridView1_ValueChanging(ByVal sender As Object, ByVal e As ValueChangingEventArgs)
        If radGridView1.CurrentColumn.Name = "Employee" Then
            Dim currentValue = e.NewValue
            For Each item In radGridView1.Rows
                If item Is radGridView1.CurrentRow Then
                    Continue For
                End If
                If DirectCast(currentValue, Integer) = DirectCast(item.Cells("Employee").Value, Integer) Then
                    e.Cancel = True
                    MessageBox.Show("Employee already added")
                    Exit For
                End If
            Next item
        End If
    End Sub

    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  8. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 01 Aug in reply to Dimitar Link to this post

    Dimitar:

    This snippet does indeed prevent the adding of a duplicate Employee.  Thank you.

     

    However, it still doe not address the issue of ensuring that

     

    a) Both an Employee and Code was selected so make a "valid" row.

    b) Allowing user to cancel the add or edit row entries as needed.

    Just using the code provided , the User can add many rows without having a Code selected.

    I attempted to use cell validation, but then I get stuck not being able to cancel out of the validation.

     

     

  9. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 01 Aug in reply to James Link to this post

    UPDATE:

    I have been playing around with all day and I am getting really close...

    The below is what I have implemented, but I have discovered I am still able to populate a row without an Employee or Code if a use the Tab key

    Private Sub gv_Data_ValueChanging(sender As Object, e As ValueChangingEventArgs) Handles gv_Data.ValueChanging
     
           If gv_Data.CurrentColumn.Name = "EmployeeID" Then
     
               Dim currentValue = e.NewValue
     
               For Each item In gv_Data.Rows
     
                   If item Is gv_Data.CurrentRow Then
     
                       Continue For
     
                   End If
     
                   If String.IsNullOrEmpty(item.Cells("EmployeeID").Value.ToString) = False Then
     
                       If DirectCast(currentValue, Guid) = DirectCast(item.Cells("EmployeeID").Value, Guid) Then
     
                           e.Cancel = True
     
                           MessageBox.Show("Cannot duplicate employee on the Job Crew")
     
                           Exit For
     
                       End If
     
                   End If
     
               Next item
     
           End If
     
       End Sub
     
       Private Sub gv_Data_RowValidating(sender As Object, e As RowValidatingEventArgs) Handles gv_Data.RowValidating
     
           ' CLEAR ANY CURRENT GUI INDICATION OF ROW ERROR
     
           e.Row.ErrorText = String.Empty
     
           ' STOP VALIDATION IF THE ESCAPE KEY WAS PRESSED
     
           If quitValidating = True Then
     
               quitValidating = False
     
               Return
     
           End If
     
           ' PERFORM THE VALIDATIONS
     
           If e.Row IsNot Nothing AndAlso e.Row.Cells(1).Value Is Nothing Then
     
               e.Row.ErrorText = "Employee selection is required"
     
               e.Cancel = True
     
           End If
     
           If e.Row IsNot Nothing AndAlso e.Row.Cells(2).Value Is Nothing Then
     
               e.Row.ErrorText = "Pay Code is required"
     
               e.Cancel = True
     
           End If
     
       End Sub
     
       Private Sub gv_Data_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles gv_Data.PreviewKeyDown
     
           ' CONTROL THE ROW VALDATION
     
           If e.KeyData = Keys.Escape Then
     
               quitValidating = True
     
           End If
     
       End Sub

     

    So again, what am I missing?

     

     

     

  10. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 02 Aug Link to this post

    Hi James,

    You can use the UserAddingRow to check if both values are selected (assuming that all already added rows have valid values):
    Private Sub RadGridView1_UserAddingRow(ByVal sender As Object, ByVal e As GridViewRowCancelEventArgs)
        Dim row = TryCast(e.Rows(0), GridViewNewRowInfo)
        If row.Cells("Employee").Value IsNot Nothing AndAlso row.Cells("PayCode").Value IsNot Nothing Then
            Return
        End If
        e.Cancel = True
        MessageBox.Show("All values must be filled")
    End Sub

    If the above code is used and only one value is selected the user can press Esc to remove the focus from the new row and go to the already added rows. In this case, it does not matter which key you are using to add the new row.
     
    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  11. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 03 Aug in reply to Dimitar Link to this post

    Dimitar:

     

    I utilized RowChanging event with the below code.  Is there any added benefit using UserAddingRow versus RowValidating?

     

    01.' PERFORM THE VALIDATIONS
    02. 
    03.        If e.Row IsNot Nothing AndAlso e.Row.Cells(1).Value Is Nothing Then
    04. 
    05.            e.Row.ErrorText = "Employee selection is required"
    06. 
    07.            Me.gv_Data.CancelEdit()
    08. 
    09.            Me.gv_Data.MasterView.TableAddNewRow.CancelAddNewRow()
    10. 
    11.            e.Cancel = True
    12. 
    13.            Exit Sub
    14. 
    15.        End If
    16. 
    17.        If e.Row IsNot Nothing AndAlso e.Row.Cells("PayCodeID").Value Is Nothing Then
    18. 
    19.            e.Row.ErrorText = "Pay code selection is required"
    20. 
    21.            Me.gv_Data.CancelEdit()
    22. 
    23.            Me.gv_Data.MasterView.TableAddNewRow.CancelAddNewRow()
    24. 
    25.            e.Cancel = True
    26. 
    27.            Exit Sub
    28. 
    29.        End If

  12. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 03 Aug in reply to James Link to this post

    I meant that I used RowValidating, not RowChanging...
  13. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 04 Aug Link to this post

    Hello James,

    Thank you for writing back.

    The difference is that once you enter the new row you will not be able to move away from it (cancel the add operation) until both values are set.

    In addition, the event will fire for all data rows, not the new row only. This is not necessary if you are sure that all rows have valid values. 

    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  14. James
    James avatar
    15 posts
    Member since:
    Feb 2016

    Posted 05 Aug in reply to Dimitar Link to this post

    Thank you Dimitar for you assistance.

    This helped me out tremendously.

Back to Top
UI for WinForms is Visual Studio 2017 Ready