本文介绍了removecurrent()仅删除datagridview上的数据,而不访问数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb.net和ms访问数据库.我有1个删除按钮,但按我的意愿却无法正常工作.它只是删除数据网格视图中的当前行,而不删除数据库中的数据.这是我的代码:

I am using vb.net and ms access for database. i have 1 delete button but it not working as I want it to be. it just delete current row in data grid view, it does not delete the data in database. this is my code:

Test123BindingSource.RemoveCurrent()

而且我也尝试了这段代码.它可用于我正在开发的另一个程序,但不适用于该程序.

and i also tried this code. it works on another program i'm developing but didnt work for this program.

Test123BindingSource.RemoveCurrent()
Test123TableAdapter.Update(ABCDEDataSet.Test123)

有谁知道如何解决这个问题?预先谢谢您!

Does anyone have any ideas how to resolve this? Thank you in advance!

推荐答案

感谢您在MSDN论坛中发帖.

Thank you for posting in MSDN forum.

在DataGridView中具有一列,该列是Access数据表中记录的ID.因此,如果您有单击事件,则在单击事件内部,您将获得选定的行,然后在这段代码中执行删除查询.以下代码段是关于 删除选中的行窗体datagridview,并且也从我的数据库中删除了,希望对您有帮助:

Have a column in your DataGridView that is ID of the record from Access data table. So if you have a click event, inside your click event you get the selected row, the perform your delete query within this bit of code. The following code snippet is about deleting the selected row form datagridview and also deleted from my database, hope it will be helpful to you:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim res As DialogResult = MessageBox.Show("Are you sure you want to Delete this Profile?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
        Dim i As Integer = DataGridView1.SelectedCells(0).RowIndex
        If res = DialogResult.OK Then
            If DataGridView1.Rows.Count > 1 And i <> DataGridView1.Rows.Count - 1 Then
                com.CommandText = "DELETE FROM Table1 WHERE ID= " & DataGridView1.SelectedRows(i).Cells(0).Value.ToString() & ""
                com.Connection = Conn
                com.ExecuteNonQuery()
                Conn.Close()
                DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(i).Index)
            End If
        Else
            MsgBox("Operation Cancelled", MsgBoxStyle.Information, " ")
        End If
    End Sub

最好的问候,

张娜达


这篇关于removecurrent()仅删除datagridview上的数据,而不访问数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:42