日期:2014-05-17 浏览次数:21069 次
private void EditingControl_KeyPress(object sender, KeyPressEventArgs e) { var dgvColDataName = dgvDetail.Columns[dgvDetail.CurrentCell.ColumnIndex].DataPropertyName.ToLower(); var Query = frmProperty.cellFomatItemList.Where(t => t.DataColName == dgvColDataName); foreach (var item in Query) { if (dgvDetail.CurrentCell.IsInEditMode && item.isNumber) { if (item.IsNegativevalue) { if (!item.isDecimal && dgvDetail.CurrentCell.IsInEditMode) { if (!(char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '-')) //只能输入数字和backspace键 { e.Handled = true; } } if (item.isDecimal && dgvDetail.CurrentCell.IsInEditMode) { TextBox tb = (TextBox)sender; string strMathchValue = tb.Text.Trim(); if (!(char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.' || e.KeyChar == '-')) //只能输入数字、backspace键、小数点 { e.Handled = true; } else { if (e.KeyChar == '.' && strMathchValue.Contains('.')) //若已经包含小数点,不允许再次输入 { e.Handled = true; } if (e.KeyChar == '-' && strMathchValue.Contains('-')) //若已经包含小数点,不允许再次输入 { e.Handled = true; } } } } else { if (!item.isDecimal && dgvDetail.CurrentCell.IsInEditMode) { if (!(char.IsDigit(e.KeyChar) || e.KeyChar == '\b')) //只能输入数字和backspace键 { e.Handled = true; } } if (item.isDecimal && dgvDetail.CurrentCell.IsInEditMode) { TextBox tb = (TextBox)sender; string strMathchValue = tb.Text.Trim(); if (!(char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.')) //只能输入数字、backspace键、小数点 { e.Handled = true; } else { if (e.KeyChar == '.' && strMathchValue.Contains('.')) //若已经包含小数点,不允许再次输入 { e.Handled = true; } } } } } } }