日期:2014-05-20  浏览次数:20857 次

DataGridView控件里编辑某个单元格后,按Enter键,怎么到右边单元格?
如题!

------解决方案--------------------
给你个参考.你照这样思路去做

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<html>
<head>
<title> </title>
<script>
document.onkeydown=show;
function show()
{
if( event.keyCode != 13 ) return;
var o = event.srcElement;
if(o.tagName.toLowerCase() == "input " && o.type.toLowerCase() == "text " )
{
if( o.parentElement.nextSibling != null)
{
o.parentElement.nextSibling.childNodes[0].focus();
}
}
}
</script>
</head>
<body>
<TABLE id= "Table1 " height= "66 " cellSpacing= "1 " cellPadding= "1 " width= "456 " align= "center "
border= "1 ">
<TR>
<TD> <INPUT id= "Text1 " type= "text " name= "Text1 "/> </TD>
<TD> <INPUT id= "Text2 " type= "text " name= "Text2 "/> </TD>
<TD> <INPUT id= "Text3 " type= "text " name= "Text3 "/> </TD>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR>
<TD> </TD>
<TD> <INPUT id= "Button1 " type= "button " value= "Button " name= "Button1 "/> </TD>
<TD> </TD>
</TR>
</TABLE>
</body>
</html>

------解决方案--------------------
我想知道
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
...{
public class CustomDataGridView : DataGridView
...{
protected override bool ProcessDialogKey(Keys keyData)
...{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
...{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}


public new bool ProcessRightKey(Keys keyData)
...{
Keys key = (keyData & Keys.KeyCode);
if (key == 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;
}

return base.ProcessRightKey(keyData);
}
return base.ProcessRightKey(keyData);
}


protected override bool ProcessDataGridViewKey(KeyEventArgs e)