DataGridView 最后一行不提交
DataGridView Last Row does not commit
到目前为止,我在这方面花费的时间太长,搜索也无助于找到我遇到的问题。
我有一个绑定到 BindingSource ExistingHAFData
的 DataGridView。当用户按下按钮时,代码遍历所有选定的行并标记记录的名称和日期 "Deleted"( 带有名称和日期的记录不会显示在活动列表;并且可以在将来取消删除)。
发生的事情是除了最后一条记录外,这对所有记录都工作正常。例如,如果用户选择了 5 条记录,则 4 条将正确更新;最后选择的行不会——实际上,为清楚起见——网格上所有选择的行都正确更新,但是当我为适配器调用更新时,只返回四个而不是五个(在数据库的数据中 table ,四个收到值,最后一个没有)。
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
在上面的代码中,DidCommit 为 True。 LinesChanged,但是比我预期的少了一个。
我尝试在最后一次更改单元格后添加类似 MainData.EndEdit()
的行,但这并没有改变行为。如果我在提交之前暂停例程并手动编辑单元格,所有行都会保存。 (这向我暗示它还没有完全退出编辑模式,但如果是这种情况,我找不到任何证据或方法来结束编辑)
@jmcilhinney 缺了一块。通过添加 ExistingHAFData.EndEdit()
,我现在将 100% 的修改行保存到数据库中。
Dim DidCommit As Boolean = MainData.CommitEdit(DataGridViewDataErrorContexts.Commit)
ExistingHAFData.EndEdit()
Dim LinesChanged As Integer = DeleteAdapter.Update(CType(ExistingHAFData.DataSource, Data.DataTable))
我一直在错误地假设问题是网格本身"causing",而不是绑定源。
到目前为止,我在这方面花费的时间太长,搜索也无助于找到我遇到的问题。
我有一个绑定到 BindingSource ExistingHAFData
的 DataGridView。当用户按下按钮时,代码遍历所有选定的行并标记记录的名称和日期 "Deleted"( 带有名称和日期的记录不会显示在活动列表;并且可以在将来取消删除)。
发生的事情是除了最后一条记录外,这对所有记录都工作正常。例如,如果用户选择了 5 条记录,则 4 条将正确更新;最后选择的行不会——实际上,为清楚起见——网格上所有选择的行都正确更新,但是当我为适配器调用更新时,只返回四个而不是五个(在数据库的数据中 table ,四个收到值,最后一个没有)。
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
在上面的代码中,DidCommit 为 True。 LinesChanged,但是比我预期的少了一个。
我尝试在最后一次更改单元格后添加类似 MainData.EndEdit()
的行,但这并没有改变行为。如果我在提交之前暂停例程并手动编辑单元格,所有行都会保存。 (这向我暗示它还没有完全退出编辑模式,但如果是这种情况,我找不到任何证据或方法来结束编辑)
@jmcilhinney 缺了一块。通过添加 ExistingHAFData.EndEdit()
,我现在将 100% 的修改行保存到数据库中。
Dim DidCommit As Boolean = MainData.CommitEdit(DataGridViewDataErrorContexts.Commit)
ExistingHAFData.EndEdit()
Dim LinesChanged As Integer = DeleteAdapter.Update(CType(ExistingHAFData.DataSource, Data.DataTable))
我一直在错误地假设问题是网格本身"causing",而不是绑定源。