我为 datagridview 上的每一行创建了一个命令按钮。
代码工作正常。
Private Sub dgv_employees_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_employees.CellClick
'If dgv_employees.Columns(e.ColumnIndex).HeaderText = "Edit" Then
If e.ColumnIndex = 16 Then
'dgv_employees.Columns(16).SortMode = DataGridViewColumnSortMode.NotSortable
Dim constr As String = "Data Source=CARSON-PC;Initial Catalog=payroll;Integrated Security=True"
Dim row As DataGridViewRow = dgv_employees.Rows(e.RowIndex)
Dim ds As New DataSet()
If MessageBox.Show(String.Format("Do you want to delete ID: {0}", row.Cells("empNum").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("DELETE FROM [dbo].[emp_personal] WHERE empNum = @empNum", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@empNum", row.Cells("empNum").Value)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindGrid()
End If
End If
End Sub
但是,每当我单击列标题时,都会出现错误。
我怎样才能解决这个问题?