如何在datagridview中编辑/删除记录?

时间:2014-04-06 17:07:00

标签: vb.net visual-studio-2010

我使用此代码使用VB.NET和SQL -12

删除datagridview中的记录
 Private Sub Delete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Delete.Click
    If MessageBox.Show("delete this item?", "DELETE!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
        Me.DataGridView.Rows.RemoveAt(Me.DataGridView.CurrentRow.)
    Else
        DataGridView.Update()
    End If
End Sub

当我使用此功能时,记录只会暂时删除,但不会永久删除数据库。我该如何永久删除记录? 我编辑字段的情况也是如此。这只是暂时的。

1 个答案:

答案 0 :(得分:0)

然后为sqldataadapter设置delete命令。代码取决于您的表的KeyID名称和类型等。

Dim connection As SqlConnection 
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
...
(here you wrote the dataadapter Fill code)
...
Dim cmdDelete As New SqlCommand("DELETE FROM Customers WHERE KeyID = @KeyID", connection)
cmdDelete.Parameters.Add("@KeyID", SqlDbType.NChar, 8, "KeyID")
adapter.DeleteCommand = command

我认为dataadapter向导应该已经为你完成了这个。