通过数据库网格查看器删除选定行
Delete selected row via database grid viewer
我需要能够通过数据网格查看器删除一行及其内容,我编写的当前代码仅删除第 0 行的内容,我无法在选定的行中执行此操作。
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim dataAdapter As New OleDbDataAdapter
Dim DataTable As New DataTable
Dim DataSet As New DataSet
Try
DataSet.Tables.Add(DataTable)
Connection.Open()
dataAdapter = New OleDbDataAdapter("SELECT * FROM Students", Connection)
dataAdapter.Fill(DataTable)
DataTable.Rows(0).BeginEdit()
DataTable.Rows(0).Delete()
DataTable.Rows(0).EndEdit()
Dim Commandbuilder As New OleDbCommandBuilder(dataAdapter)
dataAdapter.Update(DataTable)
dgrStudentDatabaseViewer.DataSource = DataTable.DefaultView
Connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Connection.Close()
End Try
End Sub
提前致谢。
使用此代码,您可以删除当前选定的行:
DataTable.Rows(DataGridView1.CurrentRow.Index).BeginEdit()
DataTable.Rows(DataGridView1.CurrentRow.Index).Delete()
DataTable.Rows(DataGridView1.CurrentRow.Index).EndEdit()
我需要能够通过数据网格查看器删除一行及其内容,我编写的当前代码仅删除第 0 行的内容,我无法在选定的行中执行此操作。
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim dataAdapter As New OleDbDataAdapter
Dim DataTable As New DataTable
Dim DataSet As New DataSet
Try
DataSet.Tables.Add(DataTable)
Connection.Open()
dataAdapter = New OleDbDataAdapter("SELECT * FROM Students", Connection)
dataAdapter.Fill(DataTable)
DataTable.Rows(0).BeginEdit()
DataTable.Rows(0).Delete()
DataTable.Rows(0).EndEdit()
Dim Commandbuilder As New OleDbCommandBuilder(dataAdapter)
dataAdapter.Update(DataTable)
dgrStudentDatabaseViewer.DataSource = DataTable.DefaultView
Connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Connection.Close()
End Try
End Sub
提前致谢。
使用此代码,您可以删除当前选定的行:
DataTable.Rows(DataGridView1.CurrentRow.Index).BeginEdit()
DataTable.Rows(DataGridView1.CurrentRow.Index).Delete()
DataTable.Rows(DataGridView1.CurrentRow.Index).EndEdit()