468,272 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

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 13000
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alex Bibiano | last post: by
2 posts views Thread by amit.vasu | last post: by
4 posts views Thread by Samuel Shulman | last post: by
2 posts views Thread by Huy Nguyen | last post: by
9 posts views Thread by Miro | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.