Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42509

Deleting Button to work for both Parent and Child Table Rows

$
0
0
The Delete on my winform is not working the way I need it to. I have a form with parent table textboxes and child table datagridview. When I press the BindingNavigatorDeleteItem button it will delete the parent record and in turn delete the child records. If I select a row in the datagridview and press the delete key it only deletes the first row's cell value not the entire row. Is there a way to have both the delete key and the BindingNavigatorDeleteItem button delete based on where the cursor is at the time they press the key or button?

Basically if the user is up in the parent table textboxes I would want it to ask if you want to delete this Order. If the user is in the datagridview then I would want it to ask if they want to delete this item. I have the code working for the parent table textboxes but I'm not sure how to figure out if the user is in the datagridview so I can have it ask the other option.

Here is my BindingNavigatorDeleteItem button code...
Code:

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Dim zPONum As Integer = PONumTextBox.Text
        Dim zMPONum As String = MPONumTextBox.Text
        If MsgBox("Are you sure you want to delete PO " & zMPONum & "?", MsgBoxStyle.YesNo, Title:="Confirm Delete") = vbYes Then
            Dim zcount = 0
            For Each row As DataRowView In Me.ItemsBindingSource.List
                If ItemUpdate.ToString <> "Yes" Then
                Else
                    zcount = zcount + 1
                    MsgBox("Cannot Delete PO with Updated Items!", MsgBoxStyle.OkOnly, "Cannot Delete PO")
                    Exit For
                End If
            Next
            If zcount = 0 Then
                ' copy child table data to history tables before parent delete
                Try
                    Using connection As New SqlConnection(My.Settings.MTConnectionString)
                        Using command As New SqlCommand("ItemsCopytoHistory", connection)
                            command.CommandType = CommandType.StoredProcedure
                            command.Parameters.Add(New SqlParameter("@Original_PONum", SqlDbType.Int)).Value = PONumTextBox.Text
                            connection.Open()
                            command.ExecuteScalar()
                        End Using
                    End Using

                Catch ex As Exception

                End Try

                Try
                    Using connection As New SqlConnection(My.Settings.MTConnectionString)
                        Using command As New SqlCommand("POInvoiceCopyToHistory", connection)
                            command.CommandType = CommandType.StoredProcedure
                            command.Parameters.Add(New SqlParameter("@Original_PONum", SqlDbType.Int)).Value = PONumTextBox.Text
                            connection.Open()
                            command.ExecuteScalar()
                        End Using
                    End Using

                Catch ex As Exception

                End Try

                PordersBindingSource.RemoveCurrent()
                Me.Validate()
                Me.PordersBindingSource.EndEdit()
                Me.ItemsBindingSource.EndEdit()
                Me.PoinvoiceBindingSource.EndEdit()
                Me.PordersTableAdapter.Delete(zPONum)
            Else

            End If
        Else

        End If
    End Sub

Thanks,

Stacy

Viewing all articles
Browse latest Browse all 42509

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>