Private Sub btn_remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_remove.Click
Dim Query As String
Dim query1 As String
Dim i As Integer = 0
i = DataGridView1.CurrentRow.Index
Dim strValueYouWant As String = String.Empty
strValueYouWant = DataGridView1.Item(0, i).Value
Dim result1 As DialogResult = MessageBox.Show("Are you sure,you want delete the selected row?", _
"Important Question", MessageBoxButtons.YesNo)
If result1 = DialogResult.Yes Then
mysqlconn.Open()
Query = "delete from table1 where show_id= '" & strValueYouWant & "'"
Dim cmd As MySqlCommand = New MySqlCommand(Query, mysqlconn)
Dim k As Integer = cmd.ExecuteNonQuery()
Query = Nothing
cmd.Dispose()
query1 = "select * from table1"
Dim cmd1 As New MySqlCommand(query1, mysqlconn)
Dim reader As MySqlDataReader = cmd1.ExecuteReader
Dim table As New DataTable()
table.Load(reader)
DataGridView1.DataSource = table
query1 = Nothing
ElseIf result1 = DialogResult.No Then
Exit Sub
End If
mysqlconn.Close()
End Sub
我在上面的代码中从组合框中选择一个项目并使用数据表创建一个表并将其显示在数据网格中。然后我点击删除按钮。该项目正在从数据库中删除,但在这里我想在运行时从组合框中删除该项目。我已经在上面的代码中使用了两个查询,一个用于选择,一个用于删除,如果我用户加载方法它不起作用,因为连接已经打开错误。