Click here to Skip to main content
15,039,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to accept only numeric value to a textbox in datagridview. i used following code insert textbox in gridview
DataGridViewTextBoxColumn txt = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(txt);
txt.HeaderText = "MARKS";
can anyone help me?
Posted
Comments
BillWoodruff 15-Feb-14 0:47am
   
The code here may be useful to you:

http://www.codeproject.com/Answers/727547/Press-Enter-Key-Code-in-Csharp-2

before the second line add a line of KeyPress event where use this

if(char.IsDigit(e.KeyChar))
{ 
    e.handle=false;
}
else
{
    e.handle=true;
}
   
Comments
agent_kruger 14-Feb-14 7:32am
   
it's just like in the case of textbox
Ahmed Tamseer 14-Feb-14 8:46am
   
it's not working
Ahmed Tamseer 15-Feb-14 2:08am
   
how to write keypress event to a textbox in a griedview.?
Why dont you use script?It's simple by using script..
Here is the sample try for it...


XML
<script type="text/javascript" language=javascript>
function CheckNumeric()
{
var key;
if(navigator.appName == 'Microsoft Internet Explorer')
key = event.keyCode;
else
key = event.which
if ( !(key >= 48 && key <= 57) && key != 8 && key != 46 && key != 36 && key != 37)
{
event.returnValue = false;
}
}
</script>



XML
[code]
//Other Gridview code
...
...
<itemtemplate>
<asp:TextBox id="txtPhone" text='<% # eval("Phone") %>' runat="server" onkeypress="CheckNumeric()"></asp:TextBox>
</itemtemplate>
[/code]



I hope now you are clear about the JavaScript validation on TextBox inside the Gridview
   
v2
i find out the answer...
thank you for your suggestion
C#
if (dataGridView1[2, i].Value != null)
                   {
                       string s = dataGridView1[2, i].Value.ToString();
                       int l1 = s.Length;
                       for (int j = 0; j < l1; j++)
                       {
                           if (!(char.IsDigit(s[j])))
                           {
                               enter = 1;
                               MessageBox.Show("Enter numric value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                               break;
                           }
                       }
   
C#
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
      {
          try
          {
              e.Control.KeyPress +=
    new KeyPressEventHandler(Control_KeyPress);
          }
          catch (Exception cx)
          {
              MessageBox.Show(cx.Message);
          }
      }
      private void Control_KeyPress(object sender, KeyPressEventArgs e)
      {

          if (!char.IsControl(e.KeyChar)
   && !char.IsDigit(e.KeyChar)
   && e.KeyChar != '.')
          {
              e.Handled = true;
          }

          // only allow one decimal point
          if (e.KeyChar == '.'
              && (sender as TextBox).Text.IndexOf('.') > -1)
          {
              e.Handled = true;
          }
      }
   

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