[原]DataGridView 回车不换行代码 AND 编辑时的字符控制
// 让 dataGridView1 在遇到回车时不响应 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (dataGridView1.Focused || dataGridView1.IsCurrentCellInEditMode) { if (keyData == Keys.Enter) { if (dataGridView1.CurrentCellAddress.X == dataGridView1.ColumnCount - 1)
// 让焦点转到下一行或者别的控件 button1.Focus(); else SendKeys.Send("{Right}"); return true; } } return base.ProcessCmdKey(ref msg, keyData); }
// 控制输入到 DataGridView 编辑单元格的字符 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { DataGridViewTextBoxEditingControl c = e.Control as DataGridViewTextBoxEditingControl; c.KeyPress += c_KeyPress; } void c_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 'a' && e.KeyChar <= 'z') { e.KeyChar = (char)(e.KeyChar - 32); } }