zoukankan      html  css  js  c++  java
  • C# :DataGridView中使按下Enter键达到与按下Tab键一样的效果?

    要使按下Enter键达到与按下Tab键一样的效果,我们需要从DataGridView中派生出一个类,写一个自定义的DataGridView控件。这里有两个方面需要考虑。一方面,当DataGridView不处于编辑状态:在这种情况下,我们需要重写OnKeyDown事件来实现我们所需要的定位逻辑。另一方面,当DataGridView处于编辑的状态下:在这种情况下,Enter键是在ProcessDialogKey事件中被处理,因此我们需要重写该事件。详见以下示例:

     

     

    代码
    class myDataGridView : DataGridView

    {

        
    protected override bool ProcessDialogKey(Keys keyData)

        {

            
    if (keyData == Keys.Enter)

            {

                
    int col = this.CurrentCell.ColumnIndex;

                
    int row = this.CurrentCell.RowIndex;

                
    if (row != this.NewRowIndex)

                {

                    
    if (col == (this.Columns.Count - 1))

                    {

                        col 
    = -1;

                        row
    ++;

                    }

                    
    this.CurrentCell = this[col + 1, row];

                }

                
    return true;

            }

            
    return base.ProcessDialogKey(keyData);

        }

     

        
    protected override void OnKeyDown(KeyEventArgs e)

        {

            
    if (e.KeyData == Keys.Enter)

            {

                
    int col = this.CurrentCell.ColumnIndex;

                
    int row = this.CurrentCell.RowIndex;

                
    if (row != this.NewRowIndex)

                {

                    
    if (col == (this.Columns.Count - 1))

                    {

                        col 
    = -1;

                        row
    ++;

                    }

                    
    this.CurrentCell = this[col + 1, row];

                }

                e.Handled 
    = true;

            }

            
    base.OnKeyDown(e);

        }

    }

    运用:

    this.dataGridView1 = new myDataGridView();
  • 相关阅读:
    vue删除表格内的数据后局部刷新页面
    git到GitHub的操作和遇到的一些问题
    git push失败
    导入小程序错误
    WebStorm安装
    Office安装时报错1907的解决方法
    转战物联网·基础篇11-物联网架构与互联网及普通硬件项目的本质差异及重点概述
    转战物联网·基础篇10-物联网架构硬件端的特点及行业应用,对初创项目的选型建议
    Windows系统Git配置教程(Git配置git config)
    Windows7安装PowerShell5.1方法(Flutter新版本需要)
  • 原文地址:https://www.cnblogs.com/Fooo/p/1622072.html
Copyright © 2011-2022 走看看