求助!!!关于DataGridView的删除问题!
这是我的删除代码:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Dim sqlcmd As New SqlClient.SqlCommand
Try
sqlcmd.Connection = New SqlClient.SqlConnection(GetDBInfo())
sqlcmd.Connection.Open()
Dim Response As MsgBoxResult
Response = MsgBox("你确认要删除此记录吗?", MsgBoxStyle.YesNo, "提示框")
If Response = MsgBoxResult.Yes Then
Try
sqlcmd.CommandText = "Delete From cangk Where cangkno= '" & CangkglDataSet5.cangk.Rows(cklxDGV.CurrentCell.RowIndex).Item("cangkno") & "'"
cklxDGV.Rows.RemoveAt(cklxDGV.CurrentCell.RowIndex)
sqlcmd.ExecuteNonQuery()
Catch ex As Exception
5: MessageBox.Show(ex.Message)
End Try
End If
Dim sqlstr As String = "SELECT cangkno, cangkname, cangkadd, cangktype FROM cangk"
Dim sqlda As SqlClient.SqlDataAdapter
sqlda = New SqlClient.SqlDataAdapter(sqlstr, GetDBInfo())
sqlda.Fill(Me.CangkglDataSet5.cangk)
sqlda.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
sqlcmd.Dispose()
End Try
End Sub
打个比方,运行的时候我删除第一行是可以的,删完了以后原来第二行的数据就变成第一行了,这时候再删这一行就会提示“不能通过已删除的行访问该行的信息”
请问各位要怎么解决,我是新手,麻烦说的详细一点,谢谢了!
[解决办法]
如果用sql删除就不要再在grid上删除了,他俩用一个就够了,sql删除后需要重新绑定数据