日期:2014-05-18 浏览次数:21083 次
private bool isEnterKey; protected override bool ProcessDialogKey(Keys keyData) { if (keyData==Keys.Enter) { return this.ProcessRightKey(keyData); } return base.ProcessDialogKey(keyData); } public new bool ProcessRightKey(Keys keyData) { if (keyData == Keys.Enter) { //第一种情况:只有一行,且当光标移到最后一列时 if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.RowCount == 1)) { base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0]; return true; } //第二种情况:有多行,且当光标移到最后一列时,移到下一行第一个单元 //if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex < (base.RowCount - 1))) //{ // base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0]; // return true; //} if ((base.CurrentCell.ColumnIndex == (VisibleColumnCount - 1)) && (base.CurrentCell.RowIndex < (base.RowCount - 1))) { base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0]; return true; } isEnterKey = true; //ProcessDataGridViewKey(new KeyEventArgs(Keys.Enter)); } return base.ProcessRightKey(keyData); } protected override bool ProcessDataGridViewKey(KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { isEnterKey = true; return this.ProcessRightKey(e.KeyData); } return base.ProcessDataGridViewKey(e); } protected override void OnCellEnter(DataGridViewCellEventArgs e) { if (isEnterKey && (!CurrentCell.Visible || CurrentCell.ReadOnly)) { SendKeys.Send("{Enter}"); } else { isEnterKey = false; base.OnCellEnter(e); } }
------解决方案--------------------
编辑完 回车会触发 CellEndEdit事件,这里处理一下
------解决方案--------------------
按楼主所所想的方法,何时跳转,可以放到CurrentCellDirtyStateChanged事件中。
------解决方案--------------------
/// <summary>
/// 实现按下Enter键是移动到下一单元格,不换行
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
SendKeys.Send("{tab}");
}
return base.ProcessCmdKey(ref msg, keyData);
}
这个是不换行的 ,你可以试着改成换行的。。。