【问题标题】:How to transfer Datagridview edit control to the next cell of the selected row when we press Enter Key当我们按 Enter 键时,如何将 Datagridview 编辑控件转移到所选行的下一个单元格
【发布时间】:2016-08-04 07:42:32
【问题描述】:

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

更多详情,我附上一张图片。

请帮助我。我为此尝试了很多逻辑。

这是我的代码

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());
}

【问题讨论】:

    标签: forms winforms c#-4.0 datagridview ado.net


    【解决方案1】:

    创建一个从 DataGridView 派生的扩展控件并覆盖 ProcessDialogKey() 方法。

    有关更多信息,请参阅here

    测试来源

    用户控制: 使用 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();
            }
        }
    }
    

    测试表单代码隐藏:

    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;
        }
    }
    

    【讨论】:

    • 这不起作用。我也在检查你的链接。我正在使用这两种逻辑,但它不起作用你能给我整个逻辑吗...
    • 我们可以在dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)中使用这个ProcessDialogKey(Keys keyData)
    • dataGridView1_CellEndEdit(...) 中控制行更改为时已晚。我将把所有测试来源都放在备注中。运行它并检查它是否有效。
    • 我刚刚将您的代码添加到 dataGridView1_SelectionChangeddataGridView1_CellEndEdit 事件处理中,一切正常。虽然这段代码不需要。 celWasEndEdit 变量的用途是什么?
    • 是的,我说这一切正常,但我的问题是当我按 Enter 键时,焦点转到下一行的单元格下方,然后自动返回上一行并选择下一个单元格。每次我编辑 Currentrow 的单元格时,编辑控件都会更改行位置,然后正常工作。但我想开发,当我按下回车键编辑控件选择下一个单元格而不离开行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2011-05-15
    • 2012-05-11
    • 2018-09-29
    • 2019-01-29
    • 2019-07-02
    相关资源
    最近更新 更多