私はこの機能を半分正しく動作させています。正しく機能している部分は、DataGridView で行を選択し、「行の削除」ボタンを使用してこの関数を呼び出すと、DataGridView から行が削除されます....ただし、行は削除されませんデータベース上。
OleDb を使用して DB から行を削除するのを手伝ってくれる人はいますか?
Function DeleteTableRow()
Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
Dim dbConnection = New OleDbConnection(TaxConnStr)
Try
Dim dbCommand As OleDbCommand = New OleDbCommand
Dim rdr2 As OleDbDataReader
Dim selectedRow = DataGridView1.SelectedRows
dbCommand.CommandText = "DELETE FROM UserCriteria WHERE RowID =" & selectedRow
If dbConnection.State = ConnectionState.Closed Then
dbConnection.Open()
End If
dbCommand.Connection = dbConnection
rdr2 = dbCommand.ExecuteReader
dbCommand.ExecuteNonQuery()
rdr2.Close()
'''Must select entire row to delete
'DataGridView1.Rows.Remove(DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex))
'''allows you to select on cell in the row to delete entire row
For Each oneCell As DataGridViewCell In DataGridView1.SelectedCells
If oneCell.Selected Then
DataGridView1.Rows.RemoveAt(oneCell.RowIndex)
End If
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
dbConnection.Close()
End Try
End Function