[原]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);
    }
}

 

posted on 2015-12-27 10:23  z5337  阅读(969)  评论(0编辑  收藏  举报