我在DATAGRIDVIEW删除当前记录与表中删除的记录不一致怎么办?
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)Dim conn As OleDbConnection
Dim adapter1 As OleDbDataAdapter
Dim dsr As DataSet
Dim sql1 As String
conn = New OleDbConnection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurDir() & "\book.mdb"
conn.Open()
sql1 = "select * from manager"
adapter1 = New OleDbDataAdapter(sql1, conn)
Dim cb As New OleDbCommandBuilder(adapter1)
dsr = New DataSet
adapter1.Fill(dsr, "manager")
dsr.Tables("manager").Rows(DataGridView1.CurrentRow.Index).Delete()
adapter1.Update(dsr, "manager")
dsr.AcceptChanges() --------------------编程问答-------------------- 直接 DataGridView1.Rows.Remove DataGridView1.CurrentRow
还有,你没有给datagridview1的datasource赋值,他怎么和数据库关联? --------------------编程问答-------------------- DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
Dim conn As OleDbConnection
Dim adapter1 As OleDbDataAdapter
Dim dsr As DataSet
Dim sql1 As String
conn = New OleDbConnection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurDir() & "\book.mdb"
conn.Open()
sql1 = "delete from manager where 名称='" & DataGridView1.CurrentRow.Cells(0).Value & "'"
Dim sele As OleDbCommand = New OleDbCommand(sql1, conn)
sele.ExecuteNonQuery()
这个代码和上面的代码样.能删除,但是数据表中删除的记录和DATAGRIDVIEW中删除的记录不一致.如何处理? --------------------编程问答-------------------- 你要删除的row都没了,CurrentRow当然和表里删除的记录不一致了。
不要在这个事件里删记录。用2005超快的:以下方式就搞定
With Me
For Each selectRow As DataGridViewRow In dgvUser.SelectedRows
dgvUser.Rows.RemoveAt(selectRow.Index)
Next
.UserTableAdapter.Update(.MyDBDataSet.user)
End With --------------------编程问答-------------------- 前提是你的dgv绑定了MyDBDataSet.user --------------------编程问答-------------------- 记录一下,方便以后查询。 --------------------编程问答-------------------- 学习一下,正好可以参考
补充:.NET技术 , VB.NET