DataGridView Not Updating

O

Octavius Khan

I have a DataGridView control on a Windows form and I am updating a dataset.
Everything works fine, and data is updating properly in the underlying
database. However, changes to the dataset that I make in code does not
appear in the DataGridView. For example, if I edit a cell of the
DataGridView, all works well with the following code:

Private Sub dg_Cases_CellEndEdit(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles dg_Cases.CellEndEdit
If Me.CasesDataSet.HasChanges Then
Me.CasesTableAdapter.Update(CasesDataSet.Cases)
Me.CasesTableAdapter.Fill(CasesDataSet.Tables("Cases"))
End If
End Sub

However, when I use the code below to delete a row, two rows are actually
deleted. Also, if I add a row in code, the new row does not appear in the
DataGridView control, although it is certainly added to the underlying
database, and it appears in the DataGridView if I reload my application:

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
CasesBindingSource.RemoveCurrent()
CasesTableAdapter.Update(CasesDataSet.Cases)
CasesTableAdapter.Fill(CasesDataSet.Cases)
End Sub

Is there something I need to do in order for changes to appear in the
DataGridView? Calling the Refresh method does not cause the changes to
appear.

Thanks in advance for your assistance.
 
C

Cor Ligthert[MVP]

Octavius,

Private Sub dg_Cases_CellEndEdit(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
dg_Cases.CellEndEdit
If Me.CasesDataSet.HasChanges Then
Me.CasesTableAdapter.Update(CasesDataSet.Cases)

Above you update the DataBase nothing in the DataSet
Me.CasesTableAdapter.Fill(CasesDataSet.Tables("Cases"))

Above you add to the table everything that is in your select string
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
CasesBindingSource.RemoveCurrent()
CasesTableAdapter.Update(CasesDataSet.Cases)

Above you update the DataTable nothing in the DataSet
CasesTableAdapter.Fill(CasesDataSet.Cases)

Above you add to the table everything that is in your select string

You can clear the DataTable before the Select as you want to see changes
done by others too. However why are you using in your Select strongly typed
code and in your update raw Dataset code?

Cor
 
C

Cor Ligthert[MVP]

Octavius,

Be aware that twice a fill, is filling two times a dataset.

If you want your code like this, then use the dataset.clear before the
second select.

However that can take time and should be avoided as there is no need for it.

Your deletes and adds should be already in your dataset, so why it is not
showed in your DataGridView would be probably another problem, that is not
direct related to the updating.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top