PopeScooby PopeScooby - 1 month ago 5
Vb.net Question

How do I Fix this Bizarre DataGridview Behavior

Ok, I'm going to try and explain what is happening as best I can. I have a Windows form Which has two datagridviews on it. When the user selects a row in the first datagridview, then the bindingsource of the second datagridview gets filtered based on the primary key of the selected row in the first datagridview. This works perfectly almost all the time. However, if the user clicks back on the first datagridview before exiting(enter/tab) the cell they are in on the second datagridview it causes the line on the second datagridview to get stuck there and not get filtered out when the new filter is applied after selection change on the first datagridview. I have checked and the column that it is filtering on has value that should be getting filtered out but it just stays there. I can't figure out where this is happening though. Here is some of the code. If anyone has any insight that would be amazing.

Dim CurrSpecLine As Integer = -2
Dim CurrTreatLine As Integer = -2
Dim SelSpecLine As Integer = 0
Dim SelSpecIdx As Integer = 0
Dim SpecCellEnd As String = ""
Dim SpecCellStart As String = ""
Dim PCell_Treat As DataGridViewCell
Dim CCell_Treat As DataGridViewCell

Private Sub Itm_Spec_DetDGV_DefaultValuesNeeded(sender As System.Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles Itm_Spec_DetDGV.DefaultValuesNeeded

Dim MaxRow As Integer = 0

For Each rws As DataGridViewRow In Me.Itm_Spec_DetDGV.Rows
If MaxRow < rws.Cells(0).Value Then MaxRow = rws.Cells(0).Value
Next

MaxRow = MaxRow + 1

With e.Row
.Cells("Itm_ItmKeyCol_Spec").Value = ItmKey
.Cells("Itm_ItmSpecKeyCol_Spec").Value = CurrSpecLine
.Cells("Itm_LoadDateCol_Spec").Value = Now()
.Cells("Itm_LoadUserCol_Spec").Value = User
.Cells("Itm_SpecDetLineCol_Spec").Value = MaxRow
End With
CurrSpecLine = CurrSpecLine - 1

End Sub

Private Sub Itm_Spec_DetDGV_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles Itm_Spec_DetDGV.SelectionChanged

Dim DGV As DataGridView = Itm_Spec_DetDGV
If DGV.SelectedCells.Count > 0 Then
Me.SelSpecLine = DGV.Rows(DGV.SelectedCells(0).RowIndex).Cells("Itm_ItmSpecKeyCol_Spec").Value
Me.SelSpecIdx = DGV.SelectedCells(0).RowIndex
Else
Me.SelSpecLine = 0
Me.SelSpecIdx = 0
End If
Dim Filt As String = "ItmSpecKey = " & CStr(Me.SelSpecLine)
UseCode = False
'Me.StgItemSpecTreatmentBindingSource.RemoveFilter()
Me.StgItemSpecTreatmentBindingSource.Filter = Filt
UseCode = True

End Sub

Private Sub Itm_Spec_DetDGV_CellEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Itm_Spec_DetDGV.CellEnter

Dim DGV As DataGridView = sender
Dim RIdx As Integer = e.RowIndex
Dim CIdx As Integer = e.ColumnIndex

If IsDBNull(DGV.Rows(RIdx).Cells(CIdx).Value) = False Then
SpecCellStart = DGV.Rows(RIdx).Cells(CIdx).Value
Else
SpecCellStart = ""
End If


End Sub

Private Sub Itm_Spec_DetDGV_CellLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Itm_Spec_DetDGV.CellLeave

Dim DGV As DataGridView = sender
Dim RIdx As Integer = e.RowIndex
Dim CIdx As Integer = e.ColumnIndex

DGV.EndEdit()
If IsDBNull(DGV.Rows(RIdx).Cells(CIdx).Value) = False Then
SpecCellEnd = DGV.Rows(RIdx).Cells(CIdx).Value
Else
SpecCellEnd = ""
End If

If CIdx = DGV.Columns("Itm_SpecDetLineCol_Spec").Index Then
If SpecCellEnd <> SpecCellStart Then
If SpecCellStart > SpecCellEnd Then
For Each PRow As DataGridViewRow In DGV.Rows
If PRow.Index <> RIdx Then
Dim RVal As Integer = PRow.Cells("Itm_SpecDetLineCol_Spec").Value
If RVal >= SpecCellEnd And RVal < SpecCellStart Then
PRow.Cells("Itm_SpecDetLineCol_Spec").Value = RVal + 1
End If
End If
Next
Else
For Each NRow As DataGridViewRow In DGV.Rows
If NRow.Index <> RIdx Then
Dim RVal As Integer = NRow.Cells("Itm_SpecDetLineCol_Spec").Value
If RVal > SpecCellStart And RVal <= SpecCellEnd Then
NRow.Cells("Itm_SpecDetLineCol_Spec").Value = RVal - 1
End If
End If
Next
End If
End If
End If

End Sub

Private Sub Itm_Spec_DetDGV_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Itm_Spec_DetDGV.CellValueChanged

Dim DGV As DataGridView = sender
Dim RIdx As Integer = e.RowIndex
Dim CIdx As Integer = e.ColumnIndex

If UseCode = True Then
If RIdx > -1 Then
ItmSpecChg = True
End If
End If

End Sub

Private Sub Itm_Spec_DetDGV_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles Itm_Spec_DetDGV.KeyDown

'Dim DGV As DataGridView = sender

'If DGV.SelectedRows.Count > 0 And e.KeyCode = 46 Then

' Dim LineKey As Integer = DGV.CurrentRow.Cells("Itm_ItmSpecKeyCol_Spec").Value
' For Each GRow As DataGridViewRow In Me.Itm_Spec_TreatTypDGV.Rows
' If GRow.Cells("Itm_ItmSpecKeyCol_Treat").Value = LineKey Then
' Me.Itm_Spec_TreatTypDGV.Rows.RemoveAt(GRow.Index)
' End If
' Next

'End If



End Sub

Private Sub Itm_Spec_DetDGV_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Itm_Spec_DetDGV.EditingControlShowing

'If Me.Itm_Spec_DetDGV.CurrentCell.ColumnIndex = Me.Itm_Spec_DetDGV.Columns("Itm_SpecLineNotesCol_Spec").Index Then
' Dim TB As TextBox = DirectCast(e.Control, TextBox)
' TB.Multiline = True
'End If

End Sub

Private Sub Itm_Spec_TreatTypDGV_DefaultValuesNeeded(sender As System.Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles Itm_Spec_TreatTypDGV.DefaultValuesNeeded

With e.Row
.Cells("Itm_ItmKeyCol_Treat").Value = ItmKey
.Cells("Itm_ItmSpecKeyCol_Treat").Value = SelSpecLine
.Cells("Itm_SpecTreatmentKeyCol_Treat").Value = CurrTreatLine
.Cells("Itm_LoadDateCol_Treat").Value = Now()
.Cells("Itm_LoadUserCol_Treat").Value = User
End With
CurrTreatLine = CurrTreatLine - 1

End Sub

Private Sub Itm_Spec_TreatTypDGV_CellEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Itm_Spec_TreatTypDGV.CellEnter

Dim DGV As DataGridView = sender
Dim RIdx As Integer = e.RowIndex
Dim CIdx As Integer = e.ColumnIndex
CCell_Treat = DGV.Rows(RIdx).Cells(CIdx)

If UseCode = True Then
Dim DDGV As DataGridView = Me.Itm_Spec_DetDGV
If DDGV.SelectedCells.Count <= 0 Then

MsgBox("You MUST select a line from the detail grid to edit it's treatments", MsgBoxStyle.OkOnly)
Me.Itm_Spec_TreatTypDGV.ClearSelection()
DDGV.Focus()

End If
End If


End Sub

Private Sub Itm_Spec_TreatTypDGV_CellLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Itm_Spec_TreatTypDGV.CellLeave

Dim DGV As DataGridView = sender
Dim RIdx As Integer = e.RowIndex
Dim CIdx As Integer = e.ColumnIndex

DGV.EndEdit()
PCell_Treat = DGV.Rows(RIdx).Cells(CIdx)

End Sub

Private Sub Itm_Spec_TreatTypDGV_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Itm_Spec_TreatTypDGV.CellValueChanged

Dim DGV As DataGridView = sender
Dim RIdx As Integer = e.RowIndex
Dim CIdx As Integer = e.ColumnIndex

If UseCode = True Then
If RIdx > -1 Then
If CIdx = DGV.Columns("Itm_SpecCoverPctCol_Treat").Index Then
DGV.EndEdit()
Dim DCell As DataGridViewTextBoxCell = DGV.Rows(RIdx).Cells(CIdx)
If IsDBNull(DCell.Value) = True Then
Exit Sub
Else
Dim UsrVal As Double = DCell.Value
Dim NewVal As Double = UsrVal / 100
UseCode = False
DGV.Rows(RIdx).Cells(CIdx).Value = NewVal
UseCode = True
End If
End If
ItmSpecChg = True
End If
End If


End Sub

Private Sub Itm_Spec_TreatTypDGV_DataError(sender As System.Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Itm_Spec_TreatTypDGV.DataError

Dim DGV As DataGridView = sender

If e.Exception.Message = "Input string was not in a correct format." Then
MsgBox("The value entered is not valid for this field", vbOKOnly, "Treatment Type Error!")
CCell_Treat.Selected = False
PCell_Treat.Selected = True
e.Cancel = True
End If
End Sub

Answer

I am still unclear as to why exactly it was happening but removing the DGV.EndEdit from the TreatTypeDGV.CellLeave fixed the problem

Comments