Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello and Hi. Good Day.



I have a question. Im using a Datagridview. One of my cell I want to enter only a numeric number. How can I do it or what is the best example of codes. Please Help me.
for ex one of my column contains no of days i want to enter their only numeric values it should not allow alphabets...

help me plz



Thanks.
Posted
Updated 10-Jul-12 1:54am
v2
Comments
AmitGajjar 10-Jul-12 8:43am    
You have already posted same question. do not post your question again and again. user Improve question to change your question detail.

Try this:
C#
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {

      if (e.ColumnIndex == dataGridView1.Columns["int1"].Index) //this is numeric column
      {
        int i;
        if (!int.TryParse(Convert.ToString(e.FormattedValue), out i))
        {
          e.Cancel = true;
          MessageBox.Show("must be numeric");
        }
      }

Refer details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx[^]
 
Share this answer
 
v2
Comments
Member 9068558 10-Jul-12 8:09am    
sorry to ask u again...i dont want any msg of validation i just want to restrict alphabets to enter by using keypress event
Prasad_Kulkarni 10-Jul-12 9:16am    
Man, what is this?? Just simply comment that line. Or should I need to tell you how to do that?
Well then move your cursor to MessageBox.Show line and simply press ctrl + c and ctrl + k to make it comments
An easy way to do this is to add a custom DataGridView column which hosts a MaskedTextBox. There's an example available here[^].
 
Share this answer
 
Comments
pradiprenushe 10-Jul-12 8:40am    
I just forget concept as i am not having hand on window application. Thanks
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
        e.Handled = true;
    }

   
}
 
Share this answer
 
Comments
Member 9068558 10-Jul-12 8:33am    
i know how to keep validation in textbox...i want to keep validation in data gridview
Hi,

You can also use javascript to check if entered value is numeric or not.

Check this link[^]

Let me know if you have any query.

Thanks
-Amit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900