我有一个datagridview,数据来自数据库。我有一个按钮来删除一行和数据库条目,当我点击按钮它删除正确的条目但隐藏它上面的行(但不删除它,这是一件好事)
这是奇怪的部分,可以通过按键盘上的F4按下按钮,当我使用F4时它不会隐藏它上面的行。
这是keydown部分:
ElseIf e.KeyCode = Keys.F4 Then
BindingNavigatorDeleteItem_Click(BindingNavigatorDeleteItem, AcceptButton)
单击删除时的代码
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
Dim strSql As String
Dim cmdSql As New OracleCommand
Dim row As DataGridViewRow = dgview.CurrentRow
Dim a As String
If dgview.Rows.Count = 0 Then
MsgBox("There are no items to remove.", MsgBoxStyle.Exclamation, "Message")
Exit Sub
End If
a = MsgBox("Are you sure you want to delete?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
If a = vbYes Then
strSql = "Delete from table where ID='" & row.Cells("ID").Value & "'"
With cmdSql
.Connection = oracon
.CommandText = strSql
.CommandType = CommandType.Text
oraread = .ExecuteReader()
oraread.Close()
End With
txtcode.Text = vbNullString
txtname.Text = vbNullString
txttelno.Text = vbNullString
txtadd.Text = vbNullString
GroupBox1.Enabled = False
cmdsave.Enabled = False
cmdcancel.Enabled = False
dgview.Rows.Remove(row)
Me.dgview.Refresh()
ElseIf vbNo Then
Exit Sub
End If
答案 0 :(得分:0)
如果您通过将DataGridView
绑定到BindingSource
来填充DataTable
,而RemoveCurrent
又绑定到RemoveAt
,那么您可以通过调用BindingSource
来删除记录删除当前行或Delete
以DataRow
上的索引删除。这将调用基础RowState
的{{1}}方法,将其Deleted
设置为Update
并将其从网格中删除。
然后,您可以在数据适配器上调用Fill
,以将该问题和任何其他挂起的更改保存回数据库。这可能与您在DataTable
上调用的数据适配器相同,而不是首先填充{{1}},也可能不是。{/ p>