Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net Datagridview - Return to edited cell

Posted on 2014-02-05
4
Medium Priority
?
586 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1600 total points
ID: 39835618
0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 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