使特定列仅接受Keypress事件中datagridview中的数值

Epl*_*ong 44 c# datagridview keypress

我需要使datagridview只接受keypress事件中特定列的数值.有没有最好的办法呢?

Muh*_*shi 66

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

例:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案!这使得他们远远不能输入无效数据.我想补充一点,使用硬编码列索引并不是一个好主意.我建议使用`if(dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns ["name"].索引) (5认同)
  • 我不是在批评这个答案,而是说一句一般性的评论:能否在列上使用默认格式化程序(如"AllowNumericOnly"等)设置某种格式化程序并不是一件好事.就像一个属性. (3认同)
  • 好答案,这里要添加的一件事是,这不会阻止用户从上下文菜单粘贴。 (2认同)

ham*_*med 30

您必须使用DataGridView.CellValidating事件,如下所示:

    private void dataGridView1_CellValidating(object sender, 
                                           DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == 1) // 1 should be your column index
        {
            int i;

            if (!int.TryParse(Convert.ToString(e.FormattedValue), out i))
            {
                e.Cancel = true;
                label1.Text ="please enter numeric";
            }
            else
            {
                // the input is numeric 
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 8

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
        if (dataGridView1.CurrentCell.ColumnIndex == 4) //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)
    { 
          // allowed only numeric value  ex.10
        //if (!char.IsControl(e.KeyChar)
        //    && !char.IsDigit(e.KeyChar))
        //{
        //    e.Handled = true;
        //}

               // allowed numeric and one dot  ex. 10.23
        if (!char.IsControl(e.KeyChar)&& !char.IsDigit(e.KeyChar)
             && e.KeyChar != '.')
        {
            e.Handled = true;
        }

        // only allow one decimal point
        if (e.KeyChar == '.'
            && (sender as TextBox).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)