Results 1 to 12 of 12

Thread: [RESOLVED] DataGridView and button click information sharing question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Resolved [RESOLVED] DataGridView and button click information sharing question

    Hi!

    I need again some advice.
    I allow the user to edit a row or add a new row in a datagridview. I then handle the dgv.Validating and dgv.Validated events so I can update the database. Now I added a Cancel button. However, when I press the cancel button, the validating and validated events are fired before I can set a variable( so that I know the user wants to cancel this edit).

    What is the correct way to get the information about the cancel_button click to the validating and validated events at the time they get handled?

    Kind regards,
    Andy

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridView and button click information sharing question

    Hello Andy,

    For anyone to assist you relevant code needs to be shown otherwise there are too many directions to go and that leads to undue guessing and wasted time.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Re: DataGridView and button click information sharing question

    Thanks Kevinstructor.
    In fact I believe I am stuck with the wrong approach in this case, that's why I didn't post any code, but here is the code.
    When I click the cancel button:
    Code:
    Private Sub cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancel.Click
           'cancelCurrentEdit is a class member variable of type Boolean
            cancelCurrentEdit = True
    
        End Sub
    And where I handle the validating and validated events:
    Code:
     Private Sub newGrid2_RowValidate(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles newGrid2.RowValidating
            If Not cancelCurrentEdit Then
               'check if all entered values are correct
            ....
           'Else do nothing
    Code:
    Private Sub newGrid2_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles newGrid2.RowValidated
            If Not cancelCurrentEdit Then
               'update database
            Else 
                'reset visual styles and make row readOnly and selectionMode to DataGridViewSelectionMode.FullRowSelect
    Now, when I edit the row and then keep my keyboard cursor in a cell and click the cancel button, the validated events get handled before my cancelButton click is handled. So my value for the cancel-check is set too late or it is even not set at all, as the event of the button click gets lost. Because a breakpoint in the cancel_click sub only works when I am not editing a cell.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridView and button click information sharing question

    Er .... um ..... this is all a bit cart before the horse anyway. The last place I would put an insert into database routine is in a validation event of any kind, not least because cancellation from an external trigger is always going to be impossible. In order to click the button you need to pass focus from the DGV which will automatically end the edit and trigger the validation which must finish before the click event can be processed. In any case I would take some time to find out exactly when row validation takes place. I'd guess that it's not when you think it is.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Re: DataGridView and button click information sharing question

    @dunfiddlin
    Thank you for your comment. My understanding about the order of the events is based on this schema from the reference:
    Enter
    GotFocus
    Leave
    Validating
    Validated
    LostFocus

    When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

    Enter
    GotFocus
    LostFocus
    Leave
    Validating
    Validated
    So if we look at the hierarchy then from inner to outer I would expect it to be cell->row->datagridview. As I want to wait for the user to finish all entries in the row, even like clicking some cells ahead and clicking some cells back, or moving in the row by keys in the same row, then I got to the row validating and validated events.
    The DataGridView.CellEndEdit event is too "low" in my opinion, as this gets called on every cell exit. Do you have some suggestion how I would get back the horse in front of the cart?
    I have started this upgrade project which I shouldn't have started at all, but now it's too late, there is no way back and I have two weeks to go, I'm still in my first form out of 6, but at least they are similar.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridView and button click information sharing question

    The DataGridView.CellEndEdit event is too "low" in my opinion, as this gets called on every cell exit.
    Well you're entitled to your opinion but the fact is that it cannot be anywhere other than it is. The reason for this is that there is only one editing control in the DGV which moves from cell to cell as you do. It therefore must complete all editing events for the cell it is leaving before entering another or leaving the control because there is no other opportunity to do this. It is, by definition an uncancellable procedure. The only possibility is to correct or delete using the normal editing methods either at the time or on a later occasion. The validation events enable you to prevent nonsense being entered in a single cell, or force a re-edit of a row where values 'clash', for example, but that's really all they should be used for.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridView and button click information sharing question

    Other options are to control validation and adding rows using a BindingSource component and DataTable events. Any time you can avoid the user interface and work directly with the data is the best way to go. I have no all encompassing example to provide but do have a article on MSDN Working with DataTable events that if you study it (see also references at the bottom of the article) and comprehend how these events work they will provide you with a path to take.

    When going this route you can manually create these events if you hand coded data operations or access these events when you have created strong typed data objects (i.e. via the IDE data wizard).

    See also How Do I: Add validation (strongly typed datasets) by Beth Massi.

    To do this right you need to spend time learning bit by bit rather than jumping in and handling the task in your current project.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Re: DataGridView and button click information sharing question

    OK, I think you misunderstood me. Of course, I don't want to switch the events necessary for the editing and validation. It's fantastic, that those validation events have been introduced, as this is an easy way to send the user back to the data he entered, so he can correct it.

    What I am looking for is, a way to cancel the edit. For example consider the following scenario:
    User starts to edit a row, enters data in a few cells and then he wants to cancel. At the same time, all the entered data is correct and all other cells are optional. So, the validation process finishes without an error. The data is ready (complete and correct) to be put into the database, but wait, the user wants to cancel.

    If I let the user finish edit the row and wait then for his action, then an update to the database also requires an action - like a save button click.

    Well, maybe I am again so much into my thinking patterns, that I don't see trees in the woods.

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridView and button click information sharing question

    The point is that there is nothing to cancel if you don't use the validation events to update the database! If the update to the database itself is left to the user's discretion, then the user has ample opportunity to undo what has been done and do what has been left undone before finally committing the record to posterity. The great advantage of the bound DGV is precisely that there is no need for haste, that records can be updated in batches and at intervals, and principally that the time of connection to the database is at an absolute minimum.

    You do seem to be stuck in a now rather old-fashioned mindset that the application exists merely to edit the database. In the brand new sparkly world of data manipulation the database is often something of a secondary concern for the most part with which contact is minimal.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Re: DataGridView and button click information sharing question

    Thank you Dunfiddlin. Now things got again a little bit more clear to me, but when would be the right time(in respect to VB.NET application programming patterns) to transfer the data from the datagridview to the database? When the form is closed? Should I define a timer to update the database every 10 minutes? I mean, if the form stays open all day long, as the person enters all incoming data, then with every hour of work the risk gets higher, that the data will never get to the database - power shortage, unintended pc reset, unintended application close(whereas this is still probably something that would get handled correctly), etc.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Re: DataGridView and button click information sharing question

    @dunfiddlin
    You could have told me, that there is such thing like a OleDbDataAdapter(yes, i've been using it, but I didn't know it's full capabilities), that I can use with my binding source (or DGV, I'm not sure yet), so that I just need to set up the Update/Insert queries and that thing manages the db updates and inserts for me. I just saw this idea in another post. WOW, this will be a lot easier. I will not have to collect the values from the cells, it will collect them for me, at least that's how I imagine it now.
    Last edited by AndyLD; May 14th, 2013 at 03:09 AM.

  12. #12
    Junior Member
    Join Date
    Apr 2013
    Posts
    17

    Re: DataGridView and button click information sharing question

    That's true, as OleDBDataAdapter has full control over inserting and updating data within your Datagridview.

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