Solved

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

Posted on 2007-03-19
2
297 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
2 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 500 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

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

948 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

22 Experts available now in Live!

Get 1:1 Help Now