Solved

VB.Net Datagridview - Return to edited cell

Posted on 2014-02-05
4
552 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 1

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

14 Experts available now in Live!

Get 1:1 Help Now