[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to stop user from entering an invalid value on a DataGridView

Posted on 2007-03-19
2
Medium Priority
?
303 Views
Last Modified: 2008-01-09
Hi,

I have a DataGridView with 7 Columns.  I have set all columns to readonly = true except the last column.  The user is supposed to enter inventory counts in the last column of the dgv.  I want to validate each entry as being numeric as soon as the user enters a value.

I added code to the 'CellLeave' function and I can successfully trap the error - but how do I force the cursor back to the cell in question (I would have called this 'setfocus' in VB6).

Here is some code:

Private Sub dgvInventoryCount_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvInventoryCount.CellLeave
       
 If e.ColumnIndex = 7 Then
            Dim CellValue As String = dgvInventoryCount.CurrentCell.Value.ToString
            If IsNumeric(CellValue) = False Then
                MessageBox.Show("Invalid Entry - must be a number", "Invalid Count Quantity")
                '*** WANT TO KEEP FOCUS ON THE CURRENT CELL HERE ***
            End If
End If
End Sub

As always, I'm open to attacking this from a different direction if my fundamental logic is flawed.

Thanks !
0
Comment
Question by:SteveB2175
[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 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 18755311
Have a look at the DataGridView's CellValidating event.  Setting e.Cancel = True in that should leave the focus in the cell with the invalid entry.

Roger
0
 

Author Comment

by:SteveB2175
ID: 18755567
That worked !!  The only other change that I had to make was that in the CellValidating event the variable 'CellValue' in my code evaluated to NULL (or nothing - don't know which).

I changed the IsNumeric check to:

If IsNumeric(e.FormattedValue) = False Then

Thanks again,

-Steve
0

Featured Post

Technology Partners: 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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