public frmDepositEdit() { InitializeComponent(); dgvCardType.AutoGenerateColumns = false; dgvCardType.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgvCardType_EditingControlShowing); } void dgvCardType_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); //委托行为 } //求实收金额。 decimal addMoney = 0; void Control_KeyPress(object sender, KeyPressEventArgs e) { //要处理的事件信息。 decimal intNumberBoxes = 0; decimal someAmount = 0; if (char.IsNumber(e.KeyChar)) { int column = dgvCardType.CurrentCellAddress.X; int row = dgvCardType.CurrentCellAddress.Y; if (column == 2) { string cellValue = Char.ToString(e.KeyChar); string test = dgvCardType[column, row].EditedFormattedValue.ToString(); test = test + cellValue; intNumberBoxes = Convert.ToDecimal(test); if (intNumberBoxes > 999) { e.Handled = true; } someAmount = decimal.Parse(dgvCardType.Rows[dgvCardType.CurrentCellAddress.Y].Cells[1].Value.ToString()); txt_现金金额.Text = (addMoney + intNumberBoxes * someAmount).ToString(); } } }