Results 1 to 8 of 8

Thread: [RESOLVED] DataGridView save a new row or any changed cell

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    123

    Resolved [RESOLVED] DataGridView save a new row or any changed cell

    Hello.

    I'm using a DGV and i'm looking for the best way to update the database whenever a new row is created OR whenever any datagrid view cell changes.

    The following code works fine for new rows and my question is how can i adapt it to include ANY other changed cells of any other rows being or not new rows.

    Code:
    If Me.PECASDataGridView.CurrentRow.IsNewRow = True Then
    Me.Validate()
                Me.PECASBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.CV_PARTSDataSet)
            End If
    Many thanks in advance for any kind help.
    Rgds,
    Octavio

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView save a new row or any changed cell

    The grid is basically irrelevant. You should be handling the appropriate events of the DataTable bound to the grid. Check out the MSDN documentation for the DataTable class to see what those events are.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    123

    Re: DataGridView save a new row or any changed cell

    Thank you for your answer. Sorry for bothering you but i'm new to this. I do not understand why the grid is irrelevant. That's where user is making changes before data is saved to database...
    This is probably a stupid question but how can i check what has changed if i did not yet save the data...

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: DataGridView save a new row or any changed cell

    When you have a DGV, the data you see on your form must be bound to a DataTable. When you change, delete or add to it, the DataTable also triggers its events and can be handled by its methods. This is what jmcilhinney is talking about. The DataTable is not the physical data on your drive, its an object of your application which in some form is connected to the data on your drive. The DGV is not really relevant because the data is in fact handled by the DataTable.
    Look into your code and find out what is the name of the TableAdapter and execute its Update method. ie:

    MYTableAdapter.Update(MyDataSet.MyTable)
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    123

    Re: DataGridView save a new row or any changed cell

    Ho, that's much clear now, thank you. So instead of using the DataGridView object shal i use the DataTable object ? The DataTable object must be programatically defined, correct? Would you be so kind and give me a small example?
    Thanks a lot.

    Octavio

  6. #6
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: DataGridView save a new row or any changed cell

    It depends on how you implemented your DGV. Chances are you just dropped it from your DataSources, if that is the case, then you do not need to define anything, it is created by the UI. Look in your code, in the load form event and there must be a statement in which the tableAdapter is filled.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    123

    Re: DataGridView save a new row or any changed cell

    I think i got it. Thanks a lot once again.
    Octavio

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] DataGridView save a new row or any changed cell

    The DataGridView is there for the user's sake, so that they can see the data and interact with it. In code, you should almost universally work with the data source, not the UI. One advantage you will find with that as you get more experience is that you can then make any changes you like to the UI without affecting that code. For instance, if you decided to use multiple TextBoxes to display one record at a time instead of a DataGridView to display them all, your code to handle this issue wouldn't change because the code doesn't involve the UI.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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