Custom buttons for adding/removing DataGridView items and validation

I have my own controls which extend DataGridView and BindingSource; I want to make another change to those - do not allow the user to add and remove rows in traditional DataGridView way, but to use add and remove buttons in BindingNavigator instead.

Everything works fine, until I start using row validation. I can make it so that the rows cannot be added as long as there exist rows that have not been validated. But I still have the following problems:
    1) a row that cannot be validated, cannot be deleted - I believe that happens because every time I try to hit the delete button, my grid control has to lose focus, so it tries to validate the current row.
    2) RowValidating is not called for the first row, unless I change something in it.

And suggestions It seems like I am trying to use a control in a way quite different from intended, but this seems to be a fairly common scenario.

Thank you,

Aleksey.


Answer this question

Custom buttons for adding/removing DataGridView items and validation

  • Ian Espiga

    Thanks a lot! I knew I was forgetting something!

  • Bill Records

    I already have AllowUserToAddRows and AllowUserToDeleteRows set to false. Like I've said, everything works as expected as long I do cancel the Row Validation in the RowValidating event.

    A validation also occurs when a user tries to do anything outside of the DataGridView, such as, in my case, click the delete button. So, the validation occurs *before* any of the delete events has a change to be fired. The worst is, there is no way for me to know whether the validation occurs because the user tries to hit delete, or for some other reason.

    Aleksey.


  • Stefan Hendricks

    RowValidating occurs only when you move from one row to another. It doesn not occur when you delete a row. Why can't you set the AllowUserToAddRows and AllowUserToDeleteRows property to false < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    If you want to go the event route, you can use the UserDeletingRow event to know when a user performs an action to cause a row to be removed.

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     


  • Kobi Reiter

    Yes that is correct. Validation occurs when you move off the DGV and to a control that has CausesValidation = true. As part of validation the row and cells are validated also. You can set CausesValidation on your custom buttons to false and the RowValidating event will not be fired.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     


  • Custom buttons for adding/removing DataGridView items and validation