【问题标题】:testbox Validation in specific row in the gridview网格视图中特定行中的测试框验证
【发布时间】:2015-05-25 07:47:39
【问题描述】:

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

【问题讨论】:

  • 尝试在代码后面使用函数 RowDataBound 执行此操作,并在行索引为 3 时应用验证规则。

标签: c# asp.net gridview webforms textbox


【解决方案1】:
  • 在 EditingControlShowing 中,检查当前单元格是否位于所需的列中。
  • 在EditingControlShowing中注册一个新的KeyPress事件(如果上述条件为真)。
  • 删除之前在 EditingControlShowing 中添加的任何 KeyPress 事件。
  • 在KeyPress事件中,检查如果key不是数字则取消输入。

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

代码库特西-Make a specific column only accept numeric value in datagridview in Keypress event

【讨论】:

  • 谢谢你,但那是所有的列,但我想在第三行的那一列上做一个测试框?
  • 您是否更改了 ColumnIndex == [Desired column No] 然后您可以编写另一个 IFLoop 来查找您选择的文本框。或者作为第二个选项,您可以使用 gridview 的 RowDataBound 事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
相关资源
最近更新 更多