-
Oct 22nd, 2018, 10:25 PM
#1
Thread Starter
New Member
How to update datagridview with database access
Last edited by royer11; Oct 23rd, 2018 at 11:19 PM.
Reason: More data
-
Oct 22nd, 2018, 10:38 PM
#2
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.
-
Oct 22nd, 2018, 11:02 PM
#3
Thread Starter
New Member
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
-
Oct 22nd, 2018, 11:33 PM
#4
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.
-
Oct 23rd, 2018, 11:53 AM
#5
Thread Starter
New Member
Re: How to update datagridview with database access
Originally Posted by jmcilhinney
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
-
Oct 23rd, 2018, 02:38 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|