I have two databound datagrid views (CheckInOutDetailDataGrivdView and CheckInOutUnitDataGridView). The tables they are bound to are CheckInOutDetail and CheckInOutUnit with CheckInOutDetail being the parent and CheckInOutunit being the child with the records being related by the field CheckInOutDetailID.
What I'm trying to acomplish is the have the column "UnitDataGridViewTextBoxColumn" in the CheckInOutDetailDataGridView give me a running total of the number of units associated with that record listed in the CheckInOUtUnitDataGridView. i have a column in the CheckInOUtUnitDataGridView called "UnitQuantityDataGridViewTextBoxColumn". This contains either a "1" or a "-1" to indicated if that item is to be added or deducted. However, for each item in the CheckInOutDetailDataGridView I want all of its child rows in the checkInOutUnitDataGridView to either all have a "1" value or all have a "-1" value.
Below is the code I'm using to try and accomplish this. It works fine until I mix a 1 with a -1. For instance If I add 3 units, each with a "1" and then add a unit with a "-1", my code counts the 3, then when it goes to count the last item, it realizes that there is a mixture of "1" and "-1" values, shows an error message and removes the last row. however, it now recounts and comes up with "5" instead of the expected 3.
i've also considered using a checkboxcolumn to indicate if it's 1 or "-1" (True for 1, false for -1) and then juct counting the rows instead of adding the values. However, I'm not sure how to go about this either.
Private Sub CheckInOutUnitDataGridView_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CheckInOutUnitDataGridView.CellEndEdit
Dim dgv As DataGridView = DirectCast(sender, DataGridView)
If dgv.Columns(e.ColumnIndex).Name = "UnitQuantityDataGridViewtextBoxColumn" Then
Dim UnitTotal As Integer
For Each row As DataGridViewRow In dgv.Rows
If row.IsNewRow Then Continue For
UnitTotal += row.Cells("UnitQuantityDataGridViewtextBoxColumn").Value
Next
CheckInOutDetailDataGridView.CurrentRow.Cells("UnitDataGridViewTextBoxColumn").Value() = UnitTotal
If Not IsDBNull(CheckInOutDetailDataGridView.CurrentRow.Cells("UnitDataGridViewTextBoxColumn").Value()) Then
MsgBox(Me.CheckInOutUnitDataGridView.RowCount() - 1)
MsgBox(Math.Abs(CheckInOutDetailDataGridView.CurrentRow.Cells("UnitDataGridViewTextBoxColumn").Value()))
If (Me.CheckInOutUnitDataGridView.RowCount() - 1) = Math.Abs(CheckInOutDetailDataGridView.CurrentRow.Cells("UnitDataGridViewTextBoxColumn").Value()) Then
Else : MsgBox("Can not add and deduct serial numbers on the same Check-Out")
'CheckInOutUnitBindingSource.RemoveCurrent()
CheckInOutUnitDataGridView.Rows.Remove(CheckInOutUnitDataGridView.CurrentRow)
For Each row As DataGridViewRow In dgv.Rows
If row.IsNewRow Then Continue For
UnitTotal += row.Cells("UnitQuantityDataGridViewtextBoxColumn").Value
Next
CheckInOutDetailDataGridView.CurrentRow.Cells("UnitDataGridViewTextBoxColumn").Value() = UnitTotal
End If
End If
End If