在gridview中,在编辑状态下禁止输入非法字符,并弹出提示。每个gridview中都有内置的文本框,如果在内置的文本框里面,禁止输入非法字符,同时并弹出提示。注意:不是模板列,不是在gridview上添加textbox控件。求解答!GridView禁止输入非法字符内置文本框

解决方案 »

  1.   

            public DataGridViewTextBoxEditingControl CellEdit = null;
    //需要绑定这个事件,这个操作你懂的
            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                CellEdit = (DataGridViewTextBoxEditingControl)e.Control; // 赋值
                CellEdit.SelectAll();
                CellEdit.KeyPress += Cells_KeyPress;
            }        private void Cells_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57) && Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 13)
                {
                    e.Handled = true;  // 输入非法就屏蔽
                    MessageBox.Show("只能输入数字");
                }
                else
                {
                    if ((Convert.ToInt32(e.KeyChar) == 46))// && (txtjg.Text.IndexOf(".") != -1))
                    {
                        e.Handled = true;
                    }
                }
            }
      

  2.   

    Valid功能仅仅是在你的文本框控件上的控制,你怎么会纠缠到GridView了呢?无端地扩大纠缠范围,自然就会提出“莫名其妙”的问题来耽误自己的时间。
      

  3.   

    哥,你在C#的发又不是在ASP.NET发
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
     ControlToValidate="txtScore" ErrorMessage="输入数字" ValidationExpression="^[0-9]*$"></asp:RegularExpressionValidator>判断
     或onblur中判断是否数字