Solved

VB.Net Datagridview - Return to edited cell

Posted on 2014-02-05
4
553 Views
Last Modified: 2014-02-07
Hi Experts.  On CeIlEndEdit I am trying to check the number of digits in the cell and if incorrect get the focus back to the cell so that the next key pressed appears in the cell to be corrected.   It works sometimes but most times the next key pressed appears in the cell that was selected after leaving the first cell.   Any help would be appreciated.

Relevantcell = DataGridView2.CurrentCell
        If DataGridView2.CurrentCell.ColumnIndex = 2 Then
            If Relevantcell.FormattedValue.ToString.Length < 10 Then
                MsgBox("The number is too short")
                DataGridView2.CurrentCell = Relevantcell
                DataGridView2.BeginEdit(True)
                Exit Sub
            End If
        End If
0
Comment
Question by:PNRT
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 400 total points
ID: 39835618
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 100 total points
ID: 39838220
why are you using 'CurrentCell'?
 
    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Dim relevantcell = CType(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex)
    End Sub

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 39838270
Thanks for the reply.  I changed the code to the following.  The messagebox appears, then the focus moves to the correct cell ready for editing but the next key stroke then appears in the cell that was clicked after the incorrect data was added.   Also, if I manually return to the cell, the error checking does not work twice.  Thanks again for the reply

Dim Relevantcell As DataGridViewCell

Relevantcell = CType(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex)
   If DataGridView2.CurrentCell.ColumnIndex = 2 Then
        If Relevantcell.FormattedValue.ToString.Length < 10 Then
             MsgBox("The number is too short")
             DataGridView2.CurrentCell = Relevantcell
             DataGridView2.BeginEdit(True)
             Exit Sub
         End If
   End If
0
 
LVL 27

Expert Comment

by:Ark
ID: 39841315
Actually CodeCruiser was correct - for your task you need validating event and setting e.Cancel=True to continue editing same cell
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now