datagridview新增列在最后_当用户按下C#中DataGridView的最后一列时如何添加新行...

当用户在C#中使用DataGridView时,如果在最后一列按下Enter键,如何跳转到下一行?文章提供了解决方案,通过自定义DataGridView类并重写ProcessCmdKey方法,在最后一行时检测到Enter键则添加新行。同时根据DataSource类型,如无DataSource只需设置AllowUserToAddRows为true。
摘要由CSDN通过智能技术生成

How do I jump to the next row when user presses enter on the last column of DataGridView?

This code also gives me an error which I need to resolve:

Index is out of range must be non-negative and less than the size of

collection.

private void DataGridView1_KeyDown(object sender, KeyEventArgs e)

{

try

{

if (e.KeyCode == Keys.Enter)

{

e.SuppressKeyPress = true;

int iColumn = DataGridView1.CurrentCell.ColumnIndex;

int iRow = DataGridView1.CurrentCell.RowIndex;

if (iColumn >= DataGridView1.Columns.Count - 2)

DataGridView1.CurrentCell = DataGridView1[0, iRow + 1];

else

DataGridView1.CurrentCell = DataGridView1[iColumn + 1, iRow];

}

}

catch (Exception ex)

{

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值