如何在Datagridview中仅将数值输入到文本框 [英] How Can I Input Only Numeric Value To A Textbox In Datagridview

查看:92
本文介绍了如何在Datagridview中仅将数值输入到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只接受datagridview中文本框的数值。我在gridview中使用以下代码插入文本框

DataGridViewTextBoxColumn txt = new DataGridViewTextBoxColumn();

dataGridView1.Columns.Add(txt);

txt .HeaderText =MARKS;

任何人都可以帮助我吗?

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?

推荐答案

在第二行之前添加一行 KeyPress事件在哪里使用这个



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;
}


为什么不使用脚本?使用脚本很简单..

这是示例试试它...





Why dont you use script?It's simple by using script..
Here is the sample try for it...


<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>










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







我希望您现在清楚Gridview中的TextBox上的JavaScript验证




I hope now you are clear about the JavaScript validation on TextBox inside the Gridview


我发现o答案...

感谢您的建议

i find out the answer...
thank you for your suggestion
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;
                           }
                       }


这篇关于如何在Datagridview中仅将数值输入到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆