none
How to cancel the selectionchanged event (for a datagridview control). RRS feed

  • Question

  • If the user moves to another record in the datagridview control I want to warn him that if he does that, it will abandon the changes he made to the current record, and give him a chance to "back out" of changing to a new record and save his changes to the current record first.

    I seem to recall in VB6 there was a way to do this. I think all you had to do was use a "cancel" command. But I can't find a way to do it in vb.net.


    Robert Homes

    Sunday, October 7, 2018 8:21 AM

Answers

  • Hi,

    try the code:

            If MessageBox.Show("Please confirm if you need to modify the data", "Modification confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.Cancel Then
                dt.RejectChanges()
            End If   

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert Homes Monday, October 8, 2018 4:43 PM
    Monday, October 8, 2018 3:29 AM
  • After the form is loaded and grid is displayed, consider handling the RowValidating event. The Cancel property of event argument allows you to keep the selection.


    • Marked as answer by Robert Homes Monday, October 8, 2018 4:43 PM
    Monday, October 8, 2018 4:46 AM

All replies

  • When does the selectionchanged event for the datagridview control fire -- before or after the change? If after, then is there another event that fires before that in which I can stop the change from proceeding?

    Robert Homes


    • Edited by Robert Homes Sunday, October 7, 2018 11:07 PM
    • Merged by Alex Li-MSFT Wednesday, October 10, 2018 3:19 AM same case
    Sunday, October 7, 2018 11:07 PM
  • Hi

    Give this a try. I have not tried it myself so can't comment on its usefulness.

    DataGridView Events


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, October 7, 2018 11:39 PM
    Sunday, October 7, 2018 11:38 PM
  • Hi,

    try the code:

            If MessageBox.Show("Please confirm if you need to modify the data", "Modification confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.Cancel Then
                dt.RejectChanges()
            End If   

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert Homes Monday, October 8, 2018 4:43 PM
    Monday, October 8, 2018 3:29 AM
  • After the form is loaded and grid is displayed, consider handling the RowValidating event. The Cancel property of event argument allows you to keep the selection.


    • Marked as answer by Robert Homes Monday, October 8, 2018 4:43 PM
    Monday, October 8, 2018 4:46 AM
  • Viorel:

    Good advice. You must know a lot about events. I thought that all events could be cancelled. But as you indicated, that is true of some events, but not all.


    Robert Homes

    Monday, October 8, 2018 6:20 PM
  • Alex,

    Your solution seems like a good one. But I adopted the one below from Viorel. Same difference, I guess. Thanks.


    Robert Homes

    Monday, October 8, 2018 6:22 PM