如何在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;
}
}