winfrom中datagridview如何使用TAB键切换行

angiexing 2009-09-22 11:33:05
rt
...全文
265 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tmd456 2009-09-22
  • 打赏
  • 举报
回复
protected override bool ProcessCmdKey(ref Message msg, Keys KeyData)
{
if (KeyData == Keys.tab)
{
SendKeys.Send("{Enter}");
return true;
}
return base.ProcessCmdKey(ref msg, KeyData);
}
Luckeryin 2009-09-22
  • 打赏
  • 举报
回复
以上两种方法看似都可行,不过我没去试
提醒一下就是:Index + 1时先检查一下看是不是最后一行了.
iamwxj 2009-09-22
  • 打赏
  • 举报
回复
从DataGridView派生一个新类,在新类中覆盖ProcessDialogKey方法

protected override bool ProcessDialogKey(Keys keyData)
{
if (key == Keys.Tab)
{
base.CurrentRow = base.Rows[base.CurrentRow.Index + 1];
return true;
}
return base.ProcessDialogKey(keyData);
}
lzsh0622 2009-09-22
  • 打赏
  • 举报
回复
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (dataGridView1.CurrentRow.Index >= dataGridView1.RowCount) return;
if (e.KeyCode == Keys.Tab)
{
DataGridViewCell cell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex + 1];
dataGridView1.CurrentCell = cell;
e.Handled = true;
}
}
jsoner 2009-09-22
  • 打赏
  • 举报
回复
帮顶.
自己没做过这样的.
angiexing 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lzsh0622 的回复:]
C# codeif (e.KeyCode== Keys.Tab|| e.KeyCode== Keys.Tab&& e.Shift==true)
{
DataGridViewCell cell;if (dataGridView1.CurrentRow.Index< dataGridView1.RowCount-1)
cell= dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex+1];else cell= dataGridView1[dataGridView1.CurrentCell.ColumnIndex,0];
dataGridView1.CurrentCell= cell;
e.Handled=true;
}
[/Quote]

不错,非常感谢!
lzsh0622 2009-09-22
  • 打赏
  • 举报
回复

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
// 13楼代码
}
lzsh0622 2009-09-22
  • 打赏
  • 举报
回复
if (e.KeyCode == Keys.Tab || e.KeyCode == Keys.Tab && e.Shift == true)
{
DataGridViewCell cell;
if (dataGridView1.CurrentRow.Index < dataGridView1.RowCount - 1)
cell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex + 1];
else cell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex, 0];
dataGridView1.CurrentCell = cell;
e.Handled = true;
}
angiexing 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 libinguest 的回复:]
引用 10 楼 angiexing 的回复:
引用 2 楼 lzsh0622 的回复:
C# codeprivatevoid dataGridView1_KeyDown(object sender, KeyEventArgs e)
{if (dataGridView1.CurrentRow.Index>= dataGridView1.RowCount-1)return;if (e.KeyCode== Keys.Tab)
    {
        DataGridViewCell cell= dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex+1];
        dataGridView1.CurrentCell= cell;
        e.Handled=true;
    }
}

谢谢回答,不过缺少对最后一行的判断,看#4楼的意见


改为
dataGridView1.CurrentRow.Index>= dataGridView1.RowCount-1
[/Quote]

你好,结果是正确的,但是如何能完善一下呢:
就是能用TAB和SHIF+TAB进行各条记录之间切换,目前第一行切换不到,而且最后一行不是记录间切换,而是最后一行单元格间切换,我想要整条切换,如何实现,谢谢!
风之影子 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 angiexing 的回复:]
引用 2 楼 lzsh0622 的回复:
C# codeprivatevoid dataGridView1_KeyDown(object sender, KeyEventArgs e)
{if (dataGridView1.CurrentRow.Index>= dataGridView1.RowCount-1)return;if (e.KeyCode== Keys.Tab)
    {
        DataGridViewCell cell= dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex+1];
        dataGridView1.CurrentCell= cell;
        e.Handled=true;
    }
}

谢谢回答,不过缺少对最后一行的判断,看#4楼的意见
[/Quote]

改为
dataGridView1.CurrentRow.Index>= dataGridView1.RowCount-1
angiexing 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lzsh0622 的回复:]
C# codeprivatevoid dataGridView1_KeyDown(object sender, KeyEventArgs e)
{if (dataGridView1.CurrentRow.Index>= dataGridView1.RowCount)return;if (e.KeyCode== Keys.Tab)
{
DataGridViewCell cell= dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex+1];
dataGridView1.CurrentCell= cell;
e.Handled=true;
}
}
[/Quote]
谢谢回答,不过缺少对最后一行的判断,看#4楼的意见
angiexing 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 luckeryin 的回复:]
以上两种方法看似都可行,不过我没去试
提醒一下就是:Index + 1时先检查一下看是不是最后一行了.
[/Quote]
好,谢谢你的提醒~~~~~~
angiexing 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lzsh0622 的回复:]
C# codeprivatevoid dataGridView1_KeyDown(object sender, KeyEventArgs e)
{if (e.KeyCode== Keys.Tab)
{
SendKeys.Send("{Enter}");
e.Handled=true;
}
}
[/Quote]

你好,你的方法可行,偷梁换柱了一下,你是想按TAB时是想ENTER的功能,但是我按ENTER时,又触发了另外 一个事件,所以此方法不可行,
风之影子 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lzsh0622 的回复:]
C# codeprivatevoid dataGridView1_KeyDown(object sender, KeyEventArgs e)
{if (e.KeyCode== Keys.Tab)
{
SendKeys.Send("{Enter}");
e.Handled=true;
}
}
[/Quote]


顶一下
lzsh0622 2009-09-22
  • 打赏
  • 举报
回复

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
SendKeys.Send("{Enter}");
e.Handled = true;
}
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧