在我的datagridview中,您可以选择一行,按下按钮后,四个字段中的两个字段的内容将被传输到另一个表。此时,应该从datagridview和它正在读取的表中删除在我的datagridview中选择的行。目前我可以将我需要的两个字段放入另一个表中,但之后很难删除此记录。
这是我的代码:
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\..\RailwayDatabase2.accdb"
Dim Con As OleDb.OleDbConnection
Dim comm As OleDbCommand
Dim rowval As Integer
Dim connect, query As String
Try
rowval = RotorDGV.CurrentCell.RowIndex
Catch ex As Exception
End Try
Dim col1 As String
Dim col3 As Date
col1 = RotorDGV.Rows(rowval).Cells(1).Value
col3 = RotorDGV.Rows(rowval).Cells(3).Value
connect = "provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\..\RailwayDatabase2.accdb"
query = "INSERT INTO tbl_shifts (EmployeeName, ShiftDate) VALUES(""" & col1 & """,""" & col3 & """);"
Con = New OleDb.OleDbConnection(connect)
comm = New OleDb.OleDbCommand(query, Con)
Con.Open()
comm.ExecuteNonQuery()
我需要删除的行当前等于RotorDGV.CurrentCell.RowIndex或'Rowval'。
有人可以告诉我如何在单击此按钮时从表格和datagridview中删除此记录。谢谢
答案 0 :(得分:0)
执行插入语句并执行查询后。
重复删除staement 和再次执行查询的过程。然后从 datagridview中删除行 >
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\..\RailwayDatabase2.accdb"
Dim Con As OleDb.OleDbConnection
Dim comm As OleDbCommand
Dim rowval As Integer
Dim connect, query As String
Dim col1 As String
Dim col3 As Date
Private Sub RotorDGV_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles RotorDGV.CellClick
Try
rowval= DataGridView1.CurrentCell.RowIndex
Catch ex As Exception
End Try
End Sub
在按钮中单击事件,请执行以下所有代码。
- 插入声明
醇>
col1 = RotorDGV.Rows(rowval).Cells(1).Value
col3 = RotorDGV.Rows(rowval).Cells(3).Value
query = "INSERT INTO tbl_shifts (EmployeeName, ShiftDate) VALUES(""" & col1 & """,""" & col3 & """);"
Con = New OleDb.OleDbConnection(connString)
comm = New OleDb.OleDbCommand(query, Con)
Try
Con.Open()
comm.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
Con.Close()
End Try
- 删除声明
醇>
query = "DELETE STAEMENT"
Con = New OleDb.OleDbConnection(connString)
comm = New OleDb.OleDbCommand(query, Con)
Try
Con.Open()
comm.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
Con.Close()
End Try
- 从Datagridview中删除行
醇>
RotorDGV.Rows.RemoveAt(rowval)
希望它有所帮助,或者至少你得到一个提示。
答案 1 :(得分:-1)
您可以运行存储过程来删除吗?我会传递删除记录所需的参数。