[英]How to use DataGridView and a button to delete a SQL entry in my DataGridView
因此,我已經建立了SQL連接並可以正常工作,它可以將數據拉取並綁定到datagridview。 我有一個更新按鈕,可將已編輯的數據推回到SQL Server。
Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DeleteButton.Click
'Delete Current Cell Data
Dim deleteCmd As String = "Delete FROM Contacts WHERE au_id = @Id;"
Dim myCommand As SqlCommand = New SqlCommand(deleteCmd)
myCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.VarChar, 11))
'Start the SqlCommand "@Id" parameter to the ID of the row that was clicked.
myCommand.Parameters("@Id").Value = DataGridView1.SelectedCells
現在,我目前正在努力使刪除按鈕起作用。 基本上,我需要它來刪除當前選定的數據行。
Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DeleteButton.Click If DataGridView1.SelectedRows.Count > 0 Then 'you may want to add a confirmation message, and if the user confirms delete DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0)) Else MessageBox.Show("Select 1 row before you hit Delete") End If End Sub
這就是我想出的! 我試圖通過SQL查詢來做這一切都是錯誤的。 只需在本地執行,然后使用我的更新按鈕即可完成更改。 鑒於最終用戶是最終用戶,這可能會更安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.