?
Solved

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

Posted on 2007-03-19
2
Medium Priority
?
302 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

777 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