473,324 Members | 2,257 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,324 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 13280
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.