Results 1 to 11 of 11

Thread: How To Use A MsgBox Before Updating DataGridView Cells?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    How To Use A MsgBox Before Updating DataGridView Cells?

    I would like to use a Yes/No MsgBox for when editing my datagridview data per cell, this will happen when I press enter or tab or any other time that it can change the data in each cell.

    So when I edit the text in a cell and press enter key or tab key.. is there any other key that you press that can also change/update the cell if pressed?
    Just want to make sure I am not missing some other key..

    right now it just updates on enter or tab key but how do I stop it from doing that and use a yes/no msgbox instead for these?

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,719

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    I'd start with the CellValidating Event. Make sure the dgv CauseValidation property is set to true.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    ok Thanks wes4dbt, CausesValidation is set to True but I would like to check the pre-edited cell text against the new entered/edited text and then have a Yes/No MsgBox..
    So lets say the Cell 1 says users First Name > "Joe" and I want to edit it to say "Joseph" I would like upon saving the new data in the cell to ask via msgbox: Would You Like To Change This From Joe To Joseph?
    Yes/No

    How do I start with this?

    How can I make it check the previously saved text data in the cell with the new text edit and then use this data in the msgbox?

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,719

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    By using the dgv cell events. There's CellEnter, CellBeginEdit. I can't remember off had if there is a property you can check to see if the current cell value has changed. If not then save the original value and compare in the validating event..

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    ok I am trying to do this in the validating event and it seems to be working but check out my code:

    Code:
    Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
            Dim Ask As MsgBoxResult = MsgBox("Are You Sure You Want To Update The Data In This Cell?", MsgBoxStyle.YesNo, "Confirm")
            If Ask = MsgBoxResult.Yes Then
            Else
                If Ask = MsgBoxResult.No Then
                    DataGridView1.CancelEdit()
                End If
            End If
        End Sub
    Notice how if msgbox.result.yes that there is no code below it? I was not sure how to call to update the cell data.. how do I do this?

    It works like this and just updates the cell data anyway..

    It just by default seems to update the cell data on enter or tab key but I wanted to make sure I am calling to update the cell data..

    Would this be ideal to call there?

    Code:
    DataGridView1.CurrentCell.DataGridView.EndEdit()
    this works but just want to make sure I am doing the right thing..

    Update: another issue I am having is that when I single click on any cell or tab key that the msgbox pops up every time. I only want the msgbox to show on enter or tab key press in the already opened/being edited cell, meaning that the user has completed editing the cell data and would only see this msgbox after they have finished by hitting enter/tab.. how can I prevent this from occurring?

    I don't want it to show just when I press Tab in the datagridview, it is showing for every time I hit tab in unopened dgv cells..
    Last edited by DreamWarrior77; Dec 7th, 2020 at 07:41 PM.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    vb.net Code:
    1. Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    2.     Dim cell = DataGridView1(e.ColumnIndex, e.RowIndex)
    3.  
    4.     If cell.IsInEditMode AndAlso
    5.        MessageBox.Show("Are you sure you want to update the data in this cell?",
    6.                        "Confirm",
    7.                        MessageBoxButtons.YesNo) = DialogResult.No Then
    8.         DataGridView1.CancelEdit()
    9.     End If
    10. End Sub
    That's it, that's all.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    Actually, this may be even better:
    vb.net Code:
    1. Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    2.     If DataGridView1.IsCurrentCellDirty AndAlso
    3.        MessageBox.Show("Are you sure you want to update the data in this cell?",
    4.                        "Confirm",
    5.                        MessageBoxButtons.YesNo) = DialogResult.No Then
    6.         DataGridView1.CancelEdit()
    7.     End If
    8. End Sub
    The previous code would still prompt if the user put the cell in edit mode but didn't change the data. This new code will only prompt if the data has changed.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    I tested both and both seem to work the same, you said:

    The previous code would still prompt if the user put the cell in edit mode but didn't change the data. This new code will only prompt if the data has changed.
    I tried it and it did not prompt the msgbox for whatever reason unless I am missing something? I pressed tab as well as enter and it did not prompt on either.

    Question: what does IsCurrentCellDirty mean?

    I did notice though that if lets say a cell says Paul and deleted the last letter l and then put it back (not that I intend to do this just saying) and then save by enter/tab it will prompt for yes/no, it is not a big deal at all just saying though..

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    Quote Originally Posted by DreamWarrior77 View Post
    I tried it and it did not prompt the msgbox for whatever reason unless I am missing something? I pressed tab as well as enter and it did not prompt on either.
    By default, a cell does not enter edit mode when it receives focus. There are various ways you can start editing a cell, including start typing, click the cell that already has focus and press the F2 key. The grid has a property that allows you to control how and when a cell enters edit mode, which can be whenever it receives focus. The first code will prompt any time you leave a cell that is in edit mode, whether or not a change has been made. If you leave a cell without ever entering edit mode, no prompt will appear.
    Quote Originally Posted by DreamWarrior77 View Post
    Question: what does IsCurrentCellDirty mean?
    Why ask me when you can just read the documentation? Given that you can just click the property in the code window and press the F1 to be taken directly to that documentation, there's really no reason not to do so. I'll give you this one but you really need to look first and ask questions later. I appreciate that you have experienced difficulties lately but still...

    The IsCurrentCellDirty tells you whether the current cell has been edited. The IsCurrentCellInEditMode property will tell you whether the current cell is currently in edit mode, regardless of whether the content has been edited or not. The IsCurrentCellDirty property will tell you whether the current cell is currently in edit mode and its content has been edited.
    Quote Originally Posted by DreamWarrior77 View Post
    I did notice though that if lets say a cell says Paul and deleted the last letter l and then put it back (not that I intend to do this just saying) and then save by enter/tab it will prompt for yes/no, it is not a big deal at all just saying though..
    I believe that the grid simply detects when a change occurs and sets a flag to indicate that the cell is dirty. It doesn't actually compare current value to the pending value so it can't detect that an edit has been undone. You could probably determine that yourself if you really wanted to. Assuming a text box cell, I think that this should work:
    vb.net Code:
    1. Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    2.     If DataGridView1.IsCurrentCellDirty AndAlso
    3.        CStr(DataGridView1.CurrentCell.Value) <> DataGridView1.EditingControl.Text AndAlso
    4.        MessageBox.Show("Are you sure you want to update the data in this cell?",
    5.                        "Confirm",
    6.                        MessageBoxButtons.YesNo) = DialogResult.No Then
    7.         DataGridView1.CancelEdit()
    8.     End If
    9. End Sub
    The EditingControl property is a reference to the control that is embedded in the current cell when it enters edit mode. You do all your editing in that embedded control rather than the grid itself. When the editing session ends, the data from the editing control is either copied back to the cell or discarded. The IsCurrentCellDirty flag would be set to True when that control raises a TextChanged event.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    ok Thanks again jmcilhinney, I am still learning to use VS and VB .net I did not know that you could click on a property and click F1 to go to the MSN entry for it.
    I will utilize this from now on, thank you for that..

    Please bear with me as I think I have some suffered some brain damage due to the meds I was taking..
    I get very bad brain fog and can hardly focus or remember things at times.

    Thank you for all your help

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: How To Use A MsgBox Before Updating DataGridView Cells?

    Quote Originally Posted by DreamWarrior77 View Post
    Please bear with me as I think I have some suffered some brain damage due to the meds I was taking..
    I get very bad brain fog and can hardly focus or remember things at times.
    Consider me bearing with you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width