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

查看:359
本文介绍了当我们按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.

请帮助我。我正在尝试很多逻辑。

Please help me. I am trying a lot of logic for this.

这里我的代码

try
{
    if (MouseButtons != 0) return;

    if (celWasEndEdit != null && dataGridView1.CurrentCell != null)
    {
        // if we are currently in the next line of last edit cell
        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;
            }
            //if we Edit the cell and press Enter the focus on the next cell
            else
            {
                iColNew = celWasEndEdit.ColumnIndex + 1;
                iRowNew = celWasEndEdit.RowIndex;
            }
            dataGridView1.CurrentCell = dataGridView1[iColNew, iRowNew];
        }
    }
    celWasEndEdit = null;
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}


推荐答案

创建扩展控件DataGridView 并覆盖ProcessDialogKey()/ em>方法。

Create an extended control derived from the DataGridView and override the ProcessDialogKey() method.

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

For additional information see here.

用户控制: strong>
使用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天全站免登陆