vb.netdatagridviewvstodataadapter

DataGridView Last Row does not commit


I've spent too long on this so far, and searches have not helped to find what I'm encountering.

I have a DataGridView that's bound to a BindingSource ExistingHAFData. When the user pushes a button, the code goes through all the selected rows and marks the name and date of when the recorded was "Deleted" (a record with a name and date does not show up in the active list; and can be undeleted in the future).

What is happening is this is working fine for all but the last record. For example, if the user selects five records, four will properly update; the last selected row will not -- actually, for clarity -- ALL selected rows on the grid properly update, but when I call Update for the adapter, only four is returned instead of five (and in the DB's data table, four received the values, the last did not).

Public DeleteAdapter As New SqlDataAdapter
Private Sub DeleteButton_Click(sender As Object, e As EventArgs) Handles DeleteButton.Click
    Dim SelectedRowCount As Integer = MainData.Rows.GetRowCount(DataGridViewElementStates.Selected)
    If SelectedRowCount > 0 AndAlso MessageBox.Show("Are you sure you want to close these requests?  If they have not already been filled, this will halt their progress.", "Close Requests", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        For Each dataRow As DataGridViewRow In MainData.SelectedRows
            dataRow.Cells("DeletedBy").Value = Common.GetFullUserName(Common.UserName.LastFirst)
            dataRow.Cells("DeletedDate").Value = Now.ToShortDateString
        Next
        DeleteAdapter.UpdateCommand = New SqlCommandBuilder(DeleteAdapter).GetUpdateCommand(True)
        Dim DidCommit As Boolean = MainData.CommitEdit(DataGridViewDataErrorContexts.Commit)
        Dim LinesChanged As Integer = DeleteAdapter.Update(CType(ExistingHAFData.DataSource, Data.DataTable))
        If LinesChanged > 0 Then
            LoadRecords(Fields.All)
            MessageBox.Show("Data successfully saved.", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("No new data was found to save to the database.  If you have made changes, be aware that they have not yet been saved.")
        End If
    Else
        MessageBox.Show("You must select at least one row before pressing Delete.", "No Rows Selected", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub

In the code above, DidCommit is True. LinesChanged, however is one less than what I'd expect.

I have tried adding lines like MainData.EndEdit() after the last cell change, but that did not change the behavior. If I pause the routine before the commit and manually edit a cell, all rows save. (this hints to me it's not fully out of Edit mode, but I can find no evidence or way to end edit if this is the case)


Solution

  • @jmcilhinney had the missing piece. By adding ExistingHAFData.EndEdit(), I am now getting 100% of the modified rows saved to the database.

            Dim DidCommit As Boolean = MainData.CommitEdit(DataGridViewDataErrorContexts.Commit)
            ExistingHAFData.EndEdit()
            Dim LinesChanged As Integer = DeleteAdapter.Update(CType(ExistingHAFData.DataSource, Data.DataTable))
    

    I had been operating on the false assumption that it was the grid itself "causing" the problem, not the bindingsource.