datagridview某列只能输入数字

  public DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit
        private void dataGridView_Deal_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            CellEdit = (DataGridViewTextBoxEditingControl)e.Control; // 赋值 CellEdit.SelectAll();

            CellEdit.KeyPress += Cells_KeyPress; // 绑定到事件

        }


        private void Cells_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (dataGridView_Deal.CurrentCellAddress.X == 4 || dataGridView_Deal.CurrentCellAddress.X == 5) // 判断当前列是不是要控制的列 我是控制的索引值为2的 列(即第三列)
            {

                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; // 输入非法就屏蔽

                }

                else
                {

                    //if ((Convert.ToInt32(e.KeyChar) == 46))
                    //{

                    //    e.Handled = true;

                    //}

                }

            }

        }

 

posted @ 2013-10-28 11:40  美丽的矩阵  阅读(578)  评论(0)    收藏  举报