I want to let others to delete selected row in the following datagrid view,
and i have written it like this.

Now when i select a row and press the delete button i can remove the selected row
but it will not update the database which meant it will not deleting the
above row.

What could be the reason and just one thing.

I have bound my datagrid view using databinding.


Please anyone can help me on this....

Private Sub dgvAllInvoices_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvAllInvoices.UserDeletingRow

        If MessageBox.Show("Do you really want to Delete this Invoice ?", "SD Technology", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then

                Try                    
                    Dim table As New DataTable()
                    Me.bindingSource = Me.dgvAllInvoices.DataSource
                    table = Me.bindingSource.DataSource
                    Me.dataAdapter.Update(table)
                    MsgBox("Selected  Invoice has been deleted", MsgBoxStyle.Information, "SD Technology")
                Catch ex As Exception
                    MsgBox("Cannot delete the above Invoice : Please contact the system administrator", MsgBoxStyle.Critical, "SD Technology")
                End Try
            Else
                e.Cancel = True
            Exit Sub

    End Sub

I want to let others to delete selected row in the following datagrid view,
and i have written it like this.

Now when i select a row and press the delete button i can remove the selected row
but it will not update the database which meant it will not deleting the
above row.

What could be the reason and just one thing.

I have bound my datagrid view using databinding.


Please anyone can help me on this....

Private Sub dgvAllInvoices_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvAllInvoices.UserDeletingRow

        If MessageBox.Show("Do you really want to Delete this Invoice ?", "SD Technology", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then

                Try                    
                    Dim table As New DataTable()
                    Me.bindingSource = Me.dgvAllInvoices.DataSource
                    table = Me.bindingSource.DataSource
                    Me.dataAdapter.Update(table)
                    MsgBox("Selected  Invoice has been deleted", MsgBoxStyle.Information, "SD Technology")
                Catch ex As Exception
                    MsgBox("Cannot delete the above Invoice : Please contact the system administrator", MsgBoxStyle.Critical, "SD Technology")
                End Try
            Else
                e.Cancel = True
            Exit Sub

    End Sub

To programmatically delete a row in the DataGridView control, you can use the Remove() method. The following code snippet removes all the selected rows in the DataGridView control:

For Each row As DataGridViewRow In DataGridView1.SelectedRows
            DataGridView1.Rows.Remove(row)
        Next

The user can also delete rows by first selecting the rows and then pressing the Delete key. By default, the deletion is done automatically without any prompting. But you may want to confirm the deletion with the user before deleting them. You can do so via the UserDeletingRow event:

Private Sub DataGridView1_UserDeletingRow( _
       ByVal sender As Object, _
       ByVal e As System.Windows.Forms. _
       DataGridViewRowCancelEventArgs) _
       Handles DataGridView1.UserDeletingRow
        If (Not e.Row.IsNewRow) Then
            Dim response As DialogResult = _
            MessageBox.Show( _
            "Are you sure you want to delete this row?", _
            "Delete row?", _
            MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2)
            If (response = DialogResult.No) Then
                e.Cancel = True
            End If
        End If
    End Sub

code is not working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.