C# 如何验证DataGridViewTextBoxColumn

C# 如何验证DataGridViewTextBoxColumn,c#,winforms,C#,Winforms,我是Windows应用程序开发新手 我有一个“数据网格视图文本框列”类型的网格视图列,允许用户输入记录。在该网格中,我有两列,分别是数量和费率。这两列应该只接受数字。我如何验证这一点 您可以这样修改Waqas代码 private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (((System.Windows.Forms.DataGridViewTextBoxEdit

我是Windows应用程序开发新手


我有一个“数据网格视图文本框列”类型的网格视图列,允许用户输入记录。在该网格中,我有两列,分别是数量和费率。这两列应该只接受数字。我如何验证这一点

您可以这样修改Waqas代码

 private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (((System.Windows.Forms.DataGridViewTextBoxEditingControl)  
         (sender)).EditingControlDataGridView.CurrentCell.ColumnIndex.ToString() ==  
         "1")//Enter your column index
        {
            if (!char.IsControl(e.KeyChar)
                  && !char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
                MessageBox.Show("Enter only Numeric Values");

            }
            else
            {
              //  MessageBox.Show("Enter only Numeric Values");
                e.Handled = true;
            }
        }
    }

希望这有帮助

试试这个。希望这有帮助

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            var value = (((DataGridView) (sender)).CurrentCell).Value;
            if (value != null)
            {
                var txt = value.ToString();
                double result;
                double.TryParse(txt, out result);
                if (result == 0)
                {
                    (((DataGridView)(sender)).CurrentCell).Value = 0;
                    MessageBox.Show("Invalid input.");
                }
            }
        }

@Kyle解决方案更接近于此,但是如果要捕获按键事件,则必须处理两个事件

///你的按键活动

private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
  // when user did not entered a number
  if (!Char.IsNumber(e.KeyChar)
        && (Keys)e.KeyChar != Keys.Back)  // check if backspace is pressed
  {
    // set handled to cancel the event to be proceed by the system
    e.Handled = true;
    // optionally indicate user that characters other than numbers are not allowed
    // MessageBox.Show("Only numbers are allowed");
  }
}
干杯@Riyaz

编辑
您需要检查
(Keys)e.KeyChar!=Keys.Back
有关更多功能键盘键的信息,请参阅msdn中有关系统windows窗体键的文章

此功能正常,但我不希望用户只输入字符。这会在文本框的焦点丢失后出现错误。我希望用户在按下字符键时给出错误@凯利斯:有可能吗。?如果是的话,请帮帮我,先生@KyleThanks非常喜欢Waqas Raja先生。先生,它工作得很好,只需要一点帮助。我想允许用户在输入错误号码时按backspace。如何做到这一点?请检查我编辑的答案。您只需检查
(Keys)e.KeyChar!=键。返回
,如果有效,请不要忘记标记为答案。。。谢谢先生,我正试图允许用户输入“.”,条件是(!Char.IsNumber(e.KeyChar)&&(Keys)e.KeyChar!=Keys.Back&(Keys)e.KeyChar!=Keys.Decimal),但它不起作用。有别的办法吗@Waqas Rajaas您可以键入点(
),为什么不直接执行此操作呢非常感谢@Waqas Raja It Woks:-)
private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
  // when user did not entered a number
  if (!Char.IsNumber(e.KeyChar)
        && (Keys)e.KeyChar != Keys.Back)  // check if backspace is pressed
  {
    // set handled to cancel the event to be proceed by the system
    e.Handled = true;
    // optionally indicate user that characters other than numbers are not allowed
    // MessageBox.Show("Only numbers are allowed");
  }
}