Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • Last Modified:

Why doesn't this code work?

I know the above question is very broad and open-ended, however, the code isn't that much. My scenario is that I have a Windows Form that contains a DataGridView (dgv). This grid is setup to accept numeric values only. The code in question is to prevent any empty cells in columns 1 & 2 which are the only editable cells in the grid. Columns 0 & 3 are ReadOnly. I have attached a screenshot to show where I am in the process.

DEFINED EVENTS:
txtTensionEdit_KeyDown
dgvTension_EditingControlShowing
dgvTension_CellValidating

======================================================

Initially, when I enter the cell, I press the delete key to clear out the cell. Then I press a direction key to test the "dgvTension_CellValidating" Event making sure the message "Cell cannot be empty!" is displayed. That works fine!

Now the problem!

I've entered numeric values in the red highlighted cell. Each time I enter a numeric value, the "txtTensionEdit_KeyDown" event fires and it passes the edits. As soon as I hit a direction key, the "dgvTension_CellValidating" fires and it still displays the message "Cell cannot be empty!" even though there clearly is data in the cell.

What am I missing here?


    Private Sub dgvTension_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvTension.EditingControlShowing
        Try
            Dim txtTensionEdit As TextBox = CType(e.Control, TextBox)

            EH.ErrorMessage = String.Empty

            RemoveHandler txtTensionEdit.KeyDown, AddressOf txtTensionEdit_KeyDown
            AddHandler txtTensionEdit.KeyDown, AddressOf txtTensionEdit_KeyDown
            AddHandler txtTensionEdit.KeyPress, AddressOf txtTensionEdit_KeyPress

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/dgvTension_EditingControlShowing() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
   '
   '
   '
    Private Sub txtTensionEdit_KeyDown(sender As Object, e As KeyEventArgs)
        Try
            EH.ErrorMessage = String.Empty

            iTensionColIDX = dgvTension.CurrentCell.ColumnIndex
            iTensionRowIDX = dgvTension.CurrentCell.RowIndex

            If iTensionRowIDX = dgvTension.RowCount - 1 Then
                blnLastRow = True
            End If

            If e.Handled = True Then
                Exit Sub
            ElseIf cboManual.Checked Then
                If e.KeyCode = Keys.Return Then
                    If iTensionRowIDX = dgvTension.RowCount - 1 Then
                        dgvTension.EndEdit()
                        dgvTension.Refresh()

                        ProcessTensionTestLoadResults(dgvTension)
                    End If
                End If
            ElseIf e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
                If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                    If e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Enter And e.KeyCode <> Keys.Decimal And e.KeyCode <> Keys.Delete Then
                        blnNonNumberedEntered = True
                    End If
                End If
            End If

            If Control.ModifierKeys = Keys.Shift Then
                blnNonNumberedEntered = True
            End If

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/txtTensionEdit_KeyDown() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
   '
   '
   '
    Private Sub dgvTension_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvTension.CellValidating
        Try
            Dim cl As DataGridViewCell = dgvTension.CurrentCell

            EH.ErrorMessage = String.Empty
            gblnEmptyCell = False

            Select Case e.RowIndex
                Case 0
                    Select Case e.ColumnIndex
                        Case 2  'Output Cell (0 Load)
                            If cl.Value = String.Empty Then
                                gblnEmptyCell = True
                                EH.ErrorMessage = "Cell cannot be empty!" & "~I"
                            End If
                    End Select
                Case Else
                    Select Case e.ColumnIndex
                        Case 1  'Output Cell 
                            If cl.Value = String.Empty Then
                                gblnEmptyCell = True
                                EH.ErrorMessage = "Cell cannot be empty!" & "~I"
                            End If
                        Case 2

                    End Select
            End Select

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/dgvTension_CellValidating() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)

        If gblnEmptyCell Then
            e.Cancel = True
        End If
    End Sub

Open in new window

0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Phillip BurtonCommented:
It would be helpful if you could add breakpoints and go through the code and let us know where execution is stopping.
0
 
BlakeMcKennaAuthor Commented:
I've attached a screenshot of stepping through the code. At this point, I've hit a direction key.
SteppingThroughCode.jpg
0
 
BlakeMcKennaAuthor Commented:
    Dim cl As DataGridViewCell = dgvTension.CurrentCell

Open in new window


My biggest question and probably most obvious is using the above statement, there should be data in the cell at this point "111"....and yet going through the code it shows that it's value is "".
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jacques Bourgeois (James Burger)Commented:
The CurrentCell is in limbo in many events. Instead of working with the CurrentCell in most event, work with the data provided through the e parameter to reference the cell:

Dim cl As DataGridViewCell = dgvTension.Item(e.ColumnIndex, e.RowIndex)

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Better yet, the value is already provided to you via "e.FormattedValue":
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating(v=vs.110).aspx

Example from MSDN:
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e _
    As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Me.dataGridView1.Rows(e.RowIndex).ErrorText = "" 
    Dim newInteger As Integer 

    ' Don't try to validate the 'new row' until finished  
    ' editing since there 
    ' is not any point in validating its initial value. 
    If dataGridView1.Rows(e.RowIndex).IsNewRow Then Return 
    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
        OrElse newInteger < 0 Then

        e.Cancel = True 
        Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer" 

    End If 
End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
James, the value hasn't been "committed" to the cell yet, so retrieving it from Item() will still show it as blank (or whatever value it was before editing).
0
 
Jacques Bourgeois (James Burger)Commented:
Ooop. You're right Mike. I simply reacted to the CurrentCell and did not see that it was in Validating. This is what happens when you cannot make your usual coffee in the morning (diesel in the water in my home town today).

You are right with e.FormattedValue.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Oh no!  No coffee?  I would be a very sad fellow...   =\
0
 
BlakeMcKennaAuthor Commented:
Thanks Mike...that worked!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now