Delete Button for Parent and Child Records

  • Thread starter Thread starter tnswalker
  • Start date Start date
T

tnswalker

Guest
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 (child) datagridview and press the delete key it only deletes the first rows 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 a parent table textbox 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/part#. I have the code working for the parent table textboxes but Im 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 so far...

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 Me.ActiveControl.Name = "" Then
Dim zpart As String = ItemsDataGridView.CurrentRow.Cells("PartNum").Value
If MsgBox("Are you sure you want to delete Part# " & zpart & "?", 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 Row with Updated Items!", MsgBoxStyle.OkOnly, "Cannot Delete Item")
Exit For
End If
Next
If zcount = 0 Then
ItemsBindingSource.RemoveCurrent()
Me.ItemsBindingSource.EndEdit()
Dim dt = PordersItemsDataSet.Tables("items")
dt.AcceptChanges()
Else

End If
Else
End If
Else
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 If
End Sub


I tried using the Me.ActiveControl.Name = to get the active control but when the cursor is in datagridview it shows nothing when I do a msgbox(Me.ActiveControl.Name) for debugging. Im a newbie so Im not sure of how to have it tell me the cursor is in the datagridview.

It does delete the child table row - but when I save and exit the form and go back in the record is back in the datagridview again. My save code for the form looks like this...

Private Sub PordersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PordersBindingNavigatorSaveItem.Click
Me.Validate()
Me.PordersBindingSource.EndEdit()
Me.PordersTableAdapter.Update(Me.PordersItemsDataSet.porders)
Me.ItemsBindingSource.EndEdit()
Me.ItemsTableAdapter.Update(Me.PordersItemsDataSet.items)
Me.PoinvoiceBindingSource.EndEdit()
Me.PoinvoiceTableAdapter.Update(Me.PordersItemsDataSet.poinvoice)
If Me.Save() Then
MsgBox("Your purchase order changes were saved.")
End If

End Sub

and

Private Function Save() As Boolean
Dim saved As Boolean = False

If Me.PordersItemsDataSet.HasChanges Then
Try

Dim pordersUpdates() As DataRow = Me.PordersItemsDataSet.porders.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
Dim itemsUpdates() As DataRow = Me.PordersItemsDataSet.items.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
Dim invoiceUpdates() As DataRow = Me.PordersItemsDataSet.poinvoice.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)

Me.PordersTableAdapter.Update(pordersUpdates)
Me.ItemsTableAdapter.Update(itemsUpdates)
Me.PoinvoiceTableAdapter.Update(invoiceUpdates)

Dim pordersDeletes() As DataRow = Me.PordersItemsDataSet.porders.Select("", "", DataViewRowState.Deleted)
Dim itemsDeletes() As DataRow = Me.PordersItemsDataSet.items.Select("", "", DataViewRowState.Deleted)
Dim invoiceDeletes() As DataRow = Me.PordersItemsDataSet.poinvoice.Select("", "", DataViewRowState.Deleted)

Me.PoinvoiceTableAdapter.Update(invoiceDeletes)
Me.ItemsTableAdapter.Update(itemsDeletes)
Me.PordersTableAdapter.Update(pordersDeletes)

saved = True

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If

Return saved

End Function

So I am wondering if the ItemsBindingSource.RemoveCurrent() isnt changing the row to deleted? My Save() code is looking for rows whos status is DataViewRowState.Deleted. That is where I am thinking the problem is.

Any idea would be greatly appreciated.

Thanks,

Stacy

Continue reading...
 
Back
Top