Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# gridview中特定行中的testbox验证_C#_Asp.net_Gridview_Webforms_Textbox - Fatal编程技术网

C# gridview中特定行中的testbox验证

C# gridview中特定行中的testbox验证,c#,asp.net,gridview,webforms,textbox,C#,Asp.net,Gridview,Webforms,Textbox,我有一个带有模板列的网格视图,在ItemTemplate中,我有一个与sqldatasource绑定的文本框,我想让第3行中的这个文本框只键入数字,在其他行中键入正常的任何内容? 在EditingControlShowing中,检查当前单元格是否位于所需列中 在EditingControlShowing中注册一个新的按键事件(如果上述条件为真) 删除先前在EditingControlShowing中添加的任何按键事件 在按键事件中,检查键是否不是数字,然后取消输入 private void d

我有一个带有模板列的网格视图,在ItemTemplate中,我有一个与sqldatasource绑定的文本框,我想让第3行中的这个文本框只键入数字,在其他行中键入正常的任何内容?

  • 在EditingControlShowing中,检查当前单元格是否位于所需列中
  • 在EditingControlShowing中注册一个新的按键事件(如果上述条件为真)
  • 删除先前在EditingControlShowing中添加的任何按键事件
  • 在按键事件中,检查键是否不是数字,然后取消输入

     private void dataGridView1_EditingControlShowing(object sender,     DataGridViewEditingControlShowingEventArgs e)
    {
    e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
    if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
    {
    TextBox tb = e.Control as TextBox;
    if (tb != null)
    {
        tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
    }
    }
    }
    
    private void Column1_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
    e.Handled = true;
    }
    }
    

Code Coutesy-

尝试在代码隐藏中使用函数RowDataBound执行此操作,如果行索引为3,则应用验证规则。谢谢,但这将构成所有列,但我想在第三行的该列上创建一个测试框。您是否更改了ColumnIndex==[所需列编号]然后您可以编写另一个IFLoop来查找您选择的文本框。或者作为第二个选项,您可以使用gridview的RowDataBound事件。