当我们按Enter键时,如何将Datagridview编辑控件传输到所选行的下一个单元格 [英] How to transfer Datagridview edit control to the next cell of the selected row when we press Enter Key

查看:851
本文介绍了当我们按Enter键时,如何将Datagridview编辑控件传输到所选行的下一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 dataGridView1_SelectionChanged dataGridView1_CellEndEdit 中使用此代码。它可以正常工作,但当我按Enter键时,编辑控制焦点跳到所选列的新行,然后自动返回到上一行并选择下一个单元格。但我想直接转到所选行的下一个单元格。

I am using this Code in dataGridView1_SelectionChanged and dataGridView1_CellEndEdit event. It works properly but when I press the enter Key firstly the edit control focus jumps on new row of selected column and then automatically goes back to the upper row and select the next cell. But I want to transfer directly to the next cell of the selected row.

有关详细信息,我附上了一张图片。

For more details I attached a picture.

>

请帮助我。

Here my code

c> try
{
if(MouseButtons!= 0)return;

if(celWasEndEdit!= null&& dataGridView1.CurrentCell!= null)
{
//如果我们当前在最后一行的下一行编辑单元格
if(dataGridView1.CurrentCell.RowIndex == celWasEndEdit.RowIndex + 1&
dataGridView1.CurrentCell.ColumnIndex == celWasEndEdit.ColumnIndex)
{
int iColNew;
int iRowNew = 0;
if(celWasEndEdit.ColumnIndex> = dataGridView1.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dataGridView1.CurrentCell.RowIndex;
}
//如果我们编辑单元格并按Enter键焦点在下一个单元格
else
{
iColNew = celWasEndEdit.ColumnIndex + 1;
iRowNew = celWasEndEdit.RowIndex;
}
dataGridView1.CurrentCell = dataGridView1 [iColNew,iRowNew];
}
}
celWasEndEdit = null;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}


推荐答案

并覆盖 ProcessDialogKey()方法。

有关其他信息,请参见此处< a>。

For additional information see here.

strong>
using System.Windows.Forms;

User control: using System.Windows.Forms;

namespace DGNextEdit
{
    public class UpdatedDataGridView : DataGridView
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                var col = CurrentCell.ColumnIndex;
                var row = CurrentCell.RowIndex;
                if (col >= ColumnCount - 1 && row < RowCount - 1)
                    row++;

                CurrentCell = this[col < ColumnCount - 1 ? col + 1 : 0, row];
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
    }
}

测试表格:

using System.Windows.Forms;
namespace DGNextEdit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

测试表单代码-behind:

namespace DGNextEdit
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new DGNextEdit.UpdatedDataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3});
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(465, 261);
            this.dataGridView1.TabIndex = 0;
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Column2";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "Column3";
            this.Column3.Name = "Column3";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(465, 261);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private UpdatedDataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
    }
}

这篇关于当我们按Enter键时,如何将Datagridview编辑控件传输到所选行的下一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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