Solved

VB.Net Datagridview - Return to edited cell

Posted on 2014-02-05
4
562 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 28

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 28

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

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.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…

830 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