Make a specific column only accept numeric value in datagridview in Keypress event

后端 未结 6 2120
傲寒
傲寒 2020-11-29 21:54

I need to make datagridview that only accept the numeric value for specific column only in keypress event. Is there any best way to do this?

6条回答
  •  孤城傲影
    2020-11-29 22:25

    You could also try this way, with accept decimals character

        private void Column1_KeyPress(object sender, KeyPressEventArgs e) 
        {
            //allow number, backspace and dot
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
    
            }
            //allow only one dot
            if (e.KeyChar == '.' && (sender as TextBox).Text.Contains("."))
            {
                e.Handled = true;
    
            }
        }
    

提交回复
热议问题