How to transfer Datagridview edit control to the n

2019-09-11 05:07发布

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.

enter image description here

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

Here my code

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

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-11 06:08

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

For additional information see here.

Test sources

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

Test form:

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

Test form code-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;
    }
}
查看更多
登录 后发表回答