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

后端 未结 6 2121
傲寒
傲寒 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:02

    You must use DataGridView.CellValidating Event like this :

        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 
                }
            }
        }
    

提交回复
热议问题