我有一个删除按钮,我试图通过单击该按钮从 gridview 和数据库中删除选定的行。但是有以下代码我得到了参数超出范围之类的错误并转换为int。
Private Sub dltButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dltButton.Click
'Dim StudentId As String
'StudentId =
'con.Open()
'cmd.CommandText = "delete from KaiyumVbStudent where StudentID = '"&StudentId&"'"
'cmd.Connection = con
Dim i As Integer = DataGridView1.SelectedRows(0).Index
DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
con = New SqlConnection(constring)
con.Open()
Me.StudentID = Convert.ToInt32(DataGridView1.SelectedRows(0).Index)
cmd = New SqlCommand("Delete from KaiyumVbStudent where StudentID = '@StudentID'", con)
cmd.ExecuteNonQuery()
Call databind()
Private Sub databind()
con = New SqlConnection(constring)
con.Open()
cmd = New SqlCommand("Select *from KaiyumVbStudent", con)
Dim dr As SqlDataReader = cmd.ExecuteReader()
dt = New DataTable()
dt.Load(dr)
Me.DataGridView1.DataSource = dt
End Sub