如果单元格为空,如何使Enter作为datagridview中的tab键工作,单元格保留错误 [英] How to make Enter to work as tab key in datagridview and cell leave error if cell is empty

查看:73
本文介绍了如果单元格为空,如何使Enter作为datagridview中的tab键工作,单元格保留错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i have 5 columns with data filled , what i need is when i press enter key it should work like tab key ,my last code works but when i edit the cell and then press enter key it moves down to next row and not next cell .
 when i move from one cell to another without entering any data in a cell i should not be able to move to another cell please help me?
For example if i don't dive any value in the cell and if i press enter or tab key i should not be able to move to another cell

推荐答案

创建一个继承自gridview的自定义控件

覆盖以下两种方法。并使用此自定义gridview而不是datagridview。

create a custom control inherit from gridview
override below two methods. and use this custom gridview instead of datagridview.
Protected Overrides Function ProcessDataGridViewKey(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
          Try
              If e.KeyCode = System.Windows.Forms.Keys.Enter Then
                  If MyBase.CurrentCell.ColumnIndex = MyBase.Columns.Count - 1 And MyBase.CurrentCell.RowIndex = MyBase.Rows.Count - 1 Then
                      Dim a As Form
                      a = DirectCast((MyBase.FindForm()), Form)'while last cell of grid it will pass focus to another control on form
                      a.Controls.Find(MyBase.AccessibleDescription, True)(0).Focus()
                  Else
                      MyBase.ProcessTabKey(e.KeyData)
                  End If
                  Return True
              End If
              Return MyBase.ProcessDataGridViewKey(e)
          Catch
          End Try
      End Function


'When edit Particular cell & then press enter
      Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
          If keyData = System.Windows.Forms.Keys.Enter Then
              If MyBase.CurrentCell.ColumnIndex = MyBase.Columns.Count - 1 And MyBase.CurrentCell.RowIndex = MyBase.Rows.Count - 1 Then
                  Dim a As Form
                  a = DirectCast((MyBase.FindForm()), Form)
                  a.Controls.Find(MyBase.AccessibleDescription, True)(0).Focus()
              Else
                  MyBase.ProcessTabKey(keyData)
              End If
              Return True
          End If
          Return MyBase.ProcessDialogKey(keyData)
      End Function



快乐编码!

:)


Happy Coding!
:)


这已经是 DataGridView 的默认行为。你不需要做任何特别的事。



-SA
This is the default behavior of DataGridView already. You don't have to do anything special.

—SA


这篇关于如果单元格为空,如何使Enter作为datagridview中的tab键工作,单元格保留错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆