private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (this.dataGridView1.CurrentCell.ColumnIndex == 4) //指定选择的字段索引 0 1.2.3.4 { TextBox tb = (TextBox)e.Control; //没指定的话就dataGridView1 内 textbox限制 tb.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress); } } private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e) { char key = e.KeyChar; int value = (int)key; if ((value >= 48 && value <= 57) || value == 46 || value == 8 || value == 43 || value == 45) e.Handled = false; else e.Handled = true; } private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != ‘.‘) { e.Handled = true; } }