Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

What events to use for DataGridView?

$
0
0
I have a functional requirement for a Windows App that I do not know how to code. What code I have is working up to a point but because of my lack of knowledge of the DGV, I do not know how to fix the issue. Below is my functional requirement.

REQUIREMENT:

Editable Cells are Columns 2-11.

A DataGridView control will be used to hold formatted decimal values taken from a Digital Multimeter device. This dgv will contain 14 Columns and an X amount of rows. Columns 0, 1, 12 and 13 will be disabled. When the form first loads, the starting position/location for the cursor will be Column 2, Row 1), which will be an empty cell (all Editable Cells will be empty upon initial load), awaiting for the User to hit the Enter/Return Key. At this point, a reading from a PLC (there is code that automatically retrieves a decimal value) will be taken and the value inserted into the Cell. The cursor must advance to the next column, not the next row unless they cursor is in the last row. The User also has the option to manually change this retrieved value by entering their own numeric value, which will be converted into a formatted decimal value. When the cursor is in the last Editable Column and a value is either retrieved or entered, the cursor must advance to the next row and be placed in the 2nd column. When the cursor is in the last row and last Editable Column and the Enter/Return Key is pressed, the cursor must stay in that location along with the value.

The problem that I am having is that when the cursor is in the last row and I enter a decimal value, that value disappears from that cell it was entered in before advancing to the next column. Basically it just needs to function the way the other rows do. Below is the code that I am currently running.

Code:

    Private Sub dgvLoadTests_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvLoadTests.CellEndEdit
        Try
            If colIDX = dgvLoadTests.Columns.Count - 1 Then      'This is the last Column in the Grid
                If rowIDX < dgvLoadTests.Rows.Count - 1 Then    'This is any row other than the last row in the Grid
                    dgvLoadTests.CurrentCell = dgvLoadTests(0, rowIDX + 1)
                End If
            Else
                If rowIDX < dgvLoadTests.Rows.Count - 1 Then
                    SendKeys.Send("{up}")
                End If
                dgvLoadTests.CurrentCell = dgvLoadTests(colIDX + 1, rowIDX)
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellEndEdit() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

    Private Sub dgvLoadTests_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvLoadTests.CellMouseClick
        Try
            iRow = dgvLoadTests.CurrentRow.Index

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellMouseClick() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub

    Private Sub dgvLoadTests_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvLoadTests.CellValidating
        Try
            EH.strRetVal = ""

            rowIDX = dgvLoadTests.CurrentCellAddress.Y 'Get Row Index
            colIDX = dgvLoadTests.CurrentCellAddress.X  'Get Column Index

            If dgvLoadTests.Item(e.ColumnIndex, e.RowIndex).IsInEditMode Then
                If Not dgvLoadTests.Item(e.ColumnIndex, e.RowIndex).ValueType Is GetType(Decimal) Then
                    Dim c As Control = dgvLoadTests.EditingControl
                    Dim cell As DataGridViewCell = dgvLoadTests.CurrentCell

                    If Not Decimal.TryParse(c.Text, Nothing) Then
                        If c.Text = "" Then
                        Else
                            EH.strRetVal = "Invalid Numeric Value!" & "~I"
                            e.Cancel = True
                        End If
                    Else
                        Select Case e.RowIndex
                            Case 0
                                cell.Value = Format(c.Text, c.Text) 'Don't format the Load Values
                            Case Else
                                cell.Value = Format(c.Text, GetDecimalPlaces(c.Text))
                        End Select
                    End If
                    'dgvLoadTests.CancelEdit()
                End If
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_CellValidating() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub

    Private Sub dgvLoadTests_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvLoadTests.KeyDown
        Try
            nonNumbered = False

            Select Case e.KeyCode
                Case Keys.Enter
                    rowIDX = dgvLoadTests.CurrentCellAddress.Y 'Get Row Index
                    colIDX = dgvLoadTests.CurrentCellAddress.X 'Get Column Index
                Case Keys.D0 To Keys.D9
                Case Keys.NumPad0 To Keys.NumPad9
                Case Keys.Delete
                    nonNumbered = False
                    dgvLoadTests.Rows(rowIDX).Cells(colIDX).Value = ""
                Case Keys.Back
                Case Else
                    nonNumbered = True
            End Select

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_KeyDown() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

    Private Sub dgvLoadTests_KeyPress(sender As Object, e As KeyPressEventArgs) Handles dgvLoadTests.KeyPress
        Try
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Or e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
                If blnOEM Then
                    If colIDX = dgvLoadTests.Columns.Count - 2 Then       
                        If rowIDX < dgvLoadTests.Rows.Count - 1 Then     
                            dgvLoadTests.CurrentCell = dgvLoadTests(2, rowIDX + 1)
                        Else
                            dgvLoadTests.Rows(rowIDX).Cells(colIDX).Value = GetDecimalPlaces("00.0000") 'Get Multimeter Reading
                        End If
                    Else
                        If rowIDX = dgvLoadTests.Rows.Count - 1 Then
                            If colIDX = dgvLoadTests.Columns.Count - 3 Then
                                dgvLoadTests.Rows(rowIDX).Cells(colIDX).Value = GetDecimalPlaces("00.0000") 'Get Multimeter Reading. TEST IS FINISHED at this point
                            Else
                                dgvLoadTests.Rows(rowIDX).Cells(colIDX).Value = GetDecimalPlaces("00.0000") 'Get Multimeter Reading
                                dgvLoadTests.CurrentCell = dgvLoadTests(colIDX + 1, rowIDX)
                            End If
                        Else
                            If colIDX >= 1 And colIDX < dgvLoadTests.Columns.Count - 2 Then
                                dgvLoadTests.Rows(rowIDX).Cells(colIDX).Value = GetDecimalPlaces("00.0000") 'Get Multimeter Reading
                            End If

                            If colIDX = dgvLoadTests.Columns.Count - 3 Then
                                dgvLoadTests.CurrentCell = dgvLoadTests(2, rowIDX + 1)
                            Else
                                dgvLoadTests.CurrentCell = dgvLoadTests(colIDX + 1, rowIDX)
                            End If
                        End If
                    End If
                Else    'Take this branch for Non-OEM Tests (a single Sensor test)
                End If
            End If

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/dgvLoadTests_KeyPress() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub


Viewing all articles
Browse latest Browse all 27554

Trending Articles



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