0 Replies - 841 Views - Last Post: 09 July 2012 - 04:50 PM Rate Topic: -----

#1 jrlittle86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-February 10

Enter Key Save DataGridView Row to XML

Posted 09 July 2012 - 04:50 PM

DataGridview has a little quirk I'm trying to iron out regarding how it handles the ENTER key.

I'm using a datagridview that has only two columns - Time and Names. The date column is set to read-only so the only field the user can add/edit is the Names field.

The result I'm looking for is that if a User wants to add a name and the time the name was entered, they would double-click the the last row (the empty row) of the datagrid and type in the NAME. When the User double-clicks the empty row the time is automatically entered in the TIME cell using DateTime.Now...

The user then types the name and hits ENTER. Hitting the ENTER key does two things:
1) It completes the datagrid and moves down to the next row.
2) It Updates an XML file with a new Element that contains The TIME and Name.

Simple enough? Ok, here's the part that's got me stumped. I have a sub that waits for the Enter Key Keypress (I've tried KeyUp, KeyDown as well with no better result). When the user types in a name and presses enter the ENTER key they get...a new row BUT the Sub doesn't actually trigger until the user hits Enter a 2nd time. So, the KeyPress event doesn't trigger unless the ENTER key is pressed as a single event?

Here's the code. I'll pare it down for readability.

'----------- Here is where the User Doubleclicks on the Row to Enter a new Name ------------
' ---- It works fine No problems here ------------------------------------

        Dim CurrentRow, TotalRows As Integer

        'Get the Row Position from the Doubleclick
        TotalRows = DataGridView1.RowCount
        CurrentRow = DataGridView1.CurrentRow.Index

        'Assign the Row position to the Labels - Test Force the DG to use these row values
        lblTotRow.Text = TotalRows.ToString
        lblCurRow.Text = CurrentRow.ToString

        Try
            If (CurrentRow = TotalRows - 1) And (DataGridView1.Item(0, CurrentRow).Value.ToString = "") Then            

                'Add the Time stamp
                DataGridView1.Item(0, CurrentRow).Value = DateTime.Now.ToString("hh:mm:ss tt")

                'Place the focus on the NAME field in the DG View so the User can enter a name.
                DataGridView1.Rows(CurrentRow).Cells(1).Selected = True
                DataGridView1.Select()

            End If

        Catch ex As Exception
                   MessageBox.Show("Selection Error: " & ex.Message)

        End Try

'----------------------------------------------------------------------------------------------------
'------------------- CHECK HERE - This is where the problem lies  -----------------------------------
'----------------------------------------------------------------------------------------------------

   Private Sub DataGridView1_Keypress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress

        '----------------------------------------------------------------------
        '  This Sub Inserts a Element to the existing XML file after the user presses ENTER
        '----------------------------------------------------------------------
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then

        Dim CurrentRow, TotalRows As Integer
        Dim Timestamp, Note As String

        'Retrieve the Row that was clicked
        TotalRows = CInt(lblTotRow.Text)
        CurrentRow = CInt(lblCurRow.Text)

        ' The Timestamp cell (column 0) contains the Time and Column 1 contains the Name
        Timestamp = DataGridView1.Item(0, CurrentRow).Value.ToString
        Name = DataGridView1.Item(1, CurrentRow).Value.ToString

        'Update the XML Log.
        ' This works fine
        AppendXML(lblFilename.Text, Timestamp, Name)

End If

    End Sub



What happens here is that the user has to press the ENTER key twice. What I'm looking for is that the user simply types in the name and presses ENTER. The row is added in the Datagrid and the XML file is updated.

Not sure if the datagrid is taking priority and not letting the Keypress be recognized or what but I sure would like to learn why it's behaving this way...

Looking forward to your responses!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1