Dim ddrc As Int16 = (Me.Temp_QuotesDataGridView.RowCount - 1)
With (Me.Temp_QuotesDataGridView)
For Xxx = ddrc To 0 Step -1
.Rows.Remove(.Rows(Xxx))
Next
End With
Me.Validate()
Me.Temp_QuotesBindingSource.EndEdit()
Me.Temp_QuotesTableAdapter.Update(Me.M_B_DATABASEDataSet)
该代码没有错,它并没有真正从数据库中删除 它显示记录已从datagridview中消失,但是当我打开数据库时,所有记录仍然存在。 我正在使用vb.net强类型访问数据库。
答案 0 :(得分:0)
我刚刚尝试了以下代码,它对我有用:
Imports System.Data.OleDb
Public Class Form1
Dim da As OleDbDataAdapter
Dim dt As DataTable
Const MyTableName = "dgvTest"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim con As New OleDbConnection( _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Users\Public\mdbTest.mdb;")
da = New OleDbDataAdapter("SELECT * FROM [" & MyTableName & "]", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
dt = New DataTable()
Me.BindingSource1.DataSource = dt
Me.DataGridView1.DataSource = Me.BindingSource1
da.Fill(dt)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim i As Long
With Me.DataGridView1
Dim NumDataRows As Long = .RowCount - IIf(.AllowUserToAddRows, 1, 0)
For i = NumDataRows - 1 To 0 Step -1
.Rows.Remove(.Rows(i))
Next
End With
da.Update(dt)
MessageBox.Show("The table has been emptied.")
Me.Close()
End Sub
End Class