Dataview not updating Dataset

R

rlucas

I am writing a program using a Dataview to populate a Datagrid (using
Tablestyles for formating columns for display). Everything displays
fine but any changes made to the grid are reflected in the Dataview
(confirmed using Dataview.Listchanged event) but not in the Dataset
(confirmed using the Datatable.RowDeleted event--it never fires when I
delete a row).
Even though I call the Datatable.AcceptChanges and Adapter.Fill
command on the dataset, when I leave the program and return the
dataset still has the old (deleted) records and none of the changes
are reflected. Have been working on this for days to no avail.

Any suggestions?

Thanks in advance,
Rich Lucas
 
W

William Ryan [eMVP]

I suspect its the timing of your acceptchanges command. Before you call
Acceptchanges...does DataSet.HasChanges reflect the changes? Also, are you
calling acceptchanges before or after the fill...that's the most likely
culprit.
 
R

rlucas

Sorry, I typed Adapter.Fill and meant Adapter.Update. I used the Fill
to get the Dataset originally from which I created the Dataview. And,
while I haven't checked the DataTable.HasChanges directly, If I have
deleted a row from the Dataview using the Datagrid shouldn't the
Dataset have changes? And why doesn't the DataTable.RowDeleted event
fire?

I'm using an Access Database (Jet 4.0) if that makes any difference.

Thanks,
Rich Lucas
 
M

Miha Markic [MVP C#]

Hi,

Sorry, I typed Adapter.Fill and meant Adapter.Update. I used the Fill
to get the Dataset originally from which I created the Dataview. And,
while I haven't checked the DataTable.HasChanges directly, If I have
deleted a row from the Dataview using the Datagrid shouldn't the
Dataset have changes?

No, if you call AcceptChanges before.
Can you show us the snippet of code where you save the data?

And why doesn't the DataTable.RowDeleted event
fire?

I'm using an Access Database (Jet 4.0) if that makes any difference.

It doesn't :) DataSets are independent of database.
 
R

rlucas

When I put the call to UpdateGuestTable() in the dvGuest_ListChanged sub I get
the Messagebox "Guest Updated!". When I restart the program. The record is still
there and no updates to other records.

Putting the call to UpdateGuestTable() the RowDeleted sub gets no Messagebox
meaning the DataRowChangeEvent never fired which I suppose means the row was not
deleted from the DataTable in the DataSet.

Rich Lucas


'***************************** Code
Friend cnVIP As OleDbConnection
Friend adpGuest As OleDbDataAdapter
Friend dsVIP As New DataSet
Friend dvGuest As DataView
Friend cmVIP As CurrencyManager

Sub Main()

System.Windows.Forms.Application.EnableVisualStyles()

DatabaseFile = Inifile.GetValue("Files", "DatabaseFile")
ZipcodeFile = Inifile.GetValue("Files", "ZipFile")

cnVIP = CreateMDBConnection(DatabaseFile)
adpGuest = New OleDbDataAdapter("SELECT * FROM Guest", cnVIP)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(adpGuest)

adpGuest.Fill(dsVIP, "Guest")

AddHandler dsVIP.Tables("Guest").RowDeleted, New
DataRowChangeEventHandler(AddressOf Row_Deleted)

dvGuest = New DataView(dsVIP.Tables("Guest"))
dvGuest.Sort = "LastName, FirstName"
dvGuest.AllowNew = False
AddHandler dvGuest.ListChanged, AddressOf dvGuest_ListChanged

frmMdi.ShowDialog()

End Sub

Friend Sub UpdateGuestTable()

dsVIP.Tables("Guest").AcceptChanges()
adpGuest.Update(dsVIP, "Guest")

MessageBox.Show("Guest updated!")

End Sub

'Handler for DataTable.DataRowChange event
Private Sub Row_Deleted(ByVal sender As Object, ByVal e As
DataRowChangeEventArgs)

MessageBox.Show("Row_Deleted Event: name= " + e.Row("name",
DataRowVersion.Original).ToString + " action= " +
e.Action.ToString)

UpdateGuestTable()

End Sub

' Handler for DataView.ListChanged event
Private Sub dvGuest_ListChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.ListChangedEventArgs)

Dim sType As String = System.Enum.GetName(e.ListChangedType.GetType(),
e.ListChangedType).ToString

If sType = "ItemDeleted" Then

Dim dr As DataRow = dsVIP.Tables("Guest").NewRow
Dim dvr As DataRowView = dvGuest.Item(cmVIP.Position - 1)

MessageBox.Show("DataView Record: " + dvr("ID").ToString + " " +
dvr("lastName").ToString + ", " + dvr("FirstName").ToString +
vbCrLf + "Dataset Record: " + dr("ID").ToString + " " +
dr("LastName").ToString + ", " + dr("FirstName").ToString)
End If

End Sub
 
M

Miha Markic [MVP C#]

See:
dsVIP.Tables("Guest").AcceptChanges()
adpGuest.Update(dsVIP, "Guest")

It should be:
adpGuest.Update(dsVIP, "Guest")
dsVIP.Tables("Guest").AcceptChanges()

because AcceptChanges consolidates changes in datatable thus should be
called only succesful update.
 

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