473,378 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Implementing a DataGridView numeric only column

I'm new to the datagridview control, and am left wondering how one goes
about restricting input to numeric only. Is it possible to trap key
presses at the cell level, and in the relevant columns, ignore them if
non-numeric?

Greg.

May 26 '06 #1
2 13285
Greg

I typcially use a javascript function to restrict users..
function numeric_onkeypress(){

//allow [0-9.,] only

var re = /[\d.,]/;

try{

if(!re.test(String.fromCharCode(event.keyCode))){

event.cancelBubble=true;

event.returnValue=false;

}

}catch(e){}

}
I also have some javascripts
here:http://www.codomatic.org/CodeSnippet...7/Default.aspx

Sreedhar
Codomatic.org - for asp.net infrastructure

May 26 '06 #2
You really don't want to validate with each KeyPress for several reason. For
example, the user might hit the wrong key by accidient, know it, and want to
backspace to correct it. In any case, this is not the model used by the
DataGridView. Use the DataGridView CellValidating event. There is an
excellent example of this on the MSDN2 Library:

http://msdn2.microsoft.com/en-us/library/ykdxa0bc.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

This is, by definition, not that.

"Greg" <gr*************@yahoo.co.uk> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I'm new to the datagridview control, and am left wondering how one goes
about restricting input to numeric only. Is it possible to trap key
presses at the cell level, and in the relevant columns, ignore them if
non-numeric?

Greg.

May 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Bibiano | last post by:
żAre there free column types to use in my DataGridView? For exemple a datapicker, a maskedit, or a numeric type I know I can do it, but I'm searching for some implemented code.
3
by: Pieter Coucke | last post by:
Hi, When a user types a non numeric-value in a numeric column in a DataGridView, and he tries to leave the cell, he gets this "Input string was not in a correct format."-exception. Is there a...
2
by: amit.vasu | last post by:
Hi I am using visual studio 2005 windows forms to create an application. The heart of the application is datagridview which reads the data from the database and display result to the user. ...
1
by: Rich | last post by:
Hello, I am reading data from a sql server table that is under replication. This table has the replicatin GUID column that is generated with replicatin. I am reading the data from a...
4
by: Samuel Shulman | last post by:
Hi For numeric columns I want to align it to the right How can I do that? Samuel
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
2
by: Huy Nguyen | last post by:
Hello, How can i force the cell to accept the input for now and flag it as error for user to correct it later. For Example: case 1. I have a column that is NOT bound to database table, then I...
9
by: Miro | last post by:
My current headache is proper is with the datagridview I am starting to realize that a DataGridView within vs2008 is not as 'robust' as a 'textboxfield' by default for example. Example: A...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.