Results 1 to 6 of 6

Thread: How to update datagridview with database access

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    How to update datagridview with database access

    Hello everyone, how can I update the data in datagridview with a button, at the moment I have created 2 forms:
    1 form is where I have the datagridview and to link to the other I have a button.
    Name:  frm1.jpg
Views: 1516
Size:  30.0 KB

    2 form to add data and when I close the data is not updated.
    Name:  frm2.jpg
Views: 1522
Size:  32.3 KB
    I had tried with
    Code:
    Private Sub ActualizarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ActualizarToolStripMenuItem.Click
            'SonidoTableAdapter.Update(AdmDataSet.Sonido)
            DataGridView1.Refresh()
        End Sub
    -----update----------------
    My forms and access to the database is as follows
    In frm2
    Name:  2.png
Views: 1215
Size:  32.1 KB
    so I pulled and dragged -- Sonido -- to the form frm2.
    Here I gave him a few tweaks and it was like that:
    Name:  1.jpg
Views: 1227
Size:  27.6 KB
    Last edited by royer11; Oct 23rd, 2018 at 11:19 PM. Reason: More data

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

    Re: How to update datagridview with database access

    It's a while since I have answered this question, but I've answered it a lot. The problem is that you are doing things the wrong way around. Your dialogue should not be saving anything to the database so there should not be anything to retrieve. What should be happening is that your dialogue is for input only and the data that gets input gets passed back to the calling form. That calling form then updates the grid first, by adding a new row to the DataTable that's bound to it. You should then be using the same data adapter that populated the DataTable in the first place to save the changes back to the database.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: How to update datagridview with database access

    Let's see if I understood, in the frm2 I add data to the access database but it turns out that it is only added to the frm2 and this does not affect the frm1 and consequently datagridview does not update the view

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to update datagridview with database access

    Correct. You are presumably using ADO.NET so there's no live connection to the database maintained. You open a connection, retrieve some data and then close the connection. The DataTable you populated is just a copy of the data stored locally and that is what you have hopefully bound to your DataGridView. If you open a connection to the database and save changes somewhere else, that has no effect on that local cache, so you don't see any changes in your DataTable. In that case, you would have retrieve the data into the DataTable again to reflect the current state of the database.

    That would be silly though, given that you had the data to start with. As I said, the correct approach is generally (although not necessarily always) to make changes to that local cache first, which will be reflected in the DataGridView immediately, and then save changes from there back to the database. You would generally use the same data adapter or table adapter to save changes as you used to retrieve the data in the first place, calling Update to save where you called Fill to retrieve.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: How to update datagridview with database access

    Quote Originally Posted by jmcilhinney View Post
    Correct. You are presumably using ADO.NET so there's no live connection to the database maintained. You open a connection, retrieve some data and then close the connection. The DataTable you populated is just a copy of the data stored locally and that is what you have hopefully bound to your DataGridView. If you open a connection to the database and save changes somewhere else, that has no effect on that local cache, so you don't see any changes in your DataTable. In that case, you would have retrieve the data into the DataTable again to reflect the current state of the database.

    That would be silly though, given that you had the data to start with. As I said, the correct approach is generally (although not necessarily always) to make changes to that local cache first, which will be reflected in the DataGridView immediately, and then save changes from there back to the database. You would generally use the same data adapter or table adapter to save changes as you used to retrieve the data in the first place, calling Update to save where you called Fill to retrieve.
    Can you write in code, please? I do not understand the handling of datagridview so much

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: How to update datagridview with database access

    I see your using TableAdapters so that's what I used in this example.

    Code:
    Public Class Form2
        Private Sub OwnersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles OwnersBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.OwnersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.WaterDataSet)
    
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'WaterDataSet.owners' table. You can move, or remove it, as needed.
            Try
                Me.OwnersTableAdapter.Fill(Me.WaterDataSet.owners)
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
    
        End Sub
    
        Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
            Dim arow As waterDataSet.ownersRow
            arow = CType(Me.WaterDataSet.owners.NewRow, waterDataSet.ownersRow)
    
            Dim frm As New Form3(arow)
            frm.ShowDialog()
            Me.WaterDataSet.owners.Rows.Add(arow)
        End Sub
    
    
        Private Sub OwnersDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles OwnersDataGridView.RowHeaderMouseDoubleClick
            Dim arow As waterDataSet.ownersRow
            arow = CType(CType(Me.OwnersBindingSource.Current, DataRowView).Row, waterDataSet.ownersRow)
    
            Dim frm As New Form3(arow)
            frm.ShowDialog()
        End Sub
    End Class
    
    Public Class Form3
    
        Private datarow As waterDataSet.ownersRow
        Public Sub New(row As waterDataSet.ownersRow)
    
            datarow = row
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
    
        End Sub
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            Me.OwnerIdTextBox.Text = datarow("ownerid").ToString
            Me.NameTextBox.Text = datarow("name").ToString
        End Sub
    
        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
            datarow("ownerid") = Me.OwnerIdTextBox.Text
            datarow("name") = Me.NameTextBox.Text
            Me.Close()
        End Sub
    End Class
    This example will add the data in Form3 to the datagridview on Form2 and you can edit a row from the dgv by double clicking the row header. The data has not been saved to the database, if you want to keep the data then you will have to save the data from Form2.

Tags for this Thread

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