我在dataGridView1_SelectionChanged
和dataGridView1_CellEndEdit
事件中使用此代码。它工作正常,但是当我按下回车键时,编辑控件焦点会跳到所选列的新行,然后自动返回上一行并选择下一个单元格。但我想直接转移到所选行的下一个单元格。
有关详细信息,我附上了一张照片。
请帮帮我。我为此尝试了很多逻辑。
这是我的代码
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());
}
答案 0 :(得分:0)
创建从 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;
}
}