本文介绍了testbox在gridview的特定行中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案


  • 在EditingControlShowing中,检查当前单元格是否位于所需的列中。 $ b
  • 在EditingControlShowing中注册KeyPress的新事件(如果上述条件为真)。
  • 删除以前在EditingControlShowing中添加的任何KeyPress事件。
  • 在KeyPress事件中,检查如果key不是数字,则取消输入。

      private void dataGridView1_EditingControlShowing(object发件人,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);



    $ b $ private void Column1_KeyPress(object sender,KeyPressEventArgs e)
    {
    if(!char.IsControl(e .KeyChar)&&!char.IsDigit(e.KeyChar))
    {
    e.Handled = true;


    code $

    $ b $ p

    Code Coutesy -


    I have a grid view with template columns and in the ItemTemplate i have textbox bounded with sqldatasource, I want make this text box in Row 3 only to type only number and on the other Rows to type normaly any thing ?

    解决方案

    • In EditingControlShowing, check that if the current cell lies in the desired column.
    • Register a new event of KeyPress in EditingControlShowing(if above condition is true).
    • Remove any KeyPress event added previously in EditingControlShowing.
    • In KeyPress event, check that if key is not digit then cancel the input.

       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-Make a specific column only accept numeric value in datagridview in Keypress event

    这篇关于testbox在gridview的特定行中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:46