Solved

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

Posted on 2007-03-19
2
295 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

12 Experts available now in Live!

Get 1:1 Help Now