DataGridView只能输入数字型

 public partial class Form2 : Form
    {
        private DataGridViewTextBoxEditingControl EditingControl;

       
        private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar.ToString()=="\b"||e.KeyChar.ToString()==".")
            {
                e.Handled = false; return;
            }
            if (!char.IsDigit(e.KeyChar)) e.Handled = true;
          
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds = GetExcelContent();
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private DataSet GetExcelContent()
        {
            string G_Str_ConnectionString = "SERVER=(local);DATABASE=db_GoodsManage;UID=sa;PWD=111111";
            SqlConnection myConn = new SqlConnection(G_Str_ConnectionString);
            string strCom = "SELECT * FROM v_UserView";
            myConn.Open();
            SqlDataAdapter myCommand = new SqlDataAdapter(strCom, myConn);
            //创建一个DataSet对象  
            DataSet myDataSet = new DataSet();
            //得到自己的DataSet对象  
            myCommand.Fill(myDataSet);
            //关闭此数据链接  
            myConn.Close();
            return myDataSet;
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
            EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
        }
    }

你可能感兴趣的:(datagridview)