winforms DataGridView C#中单元格编辑后回车键使用

sczxawaw  于 2023-03-13  发布在  C#
关注(0)|答案(3)|浏览(545)

输入单元格后,我试图转到datagridview中的下一个单元格,但它转到同一列中的下一行**如果未在单元格中输入任何内容,它工作正常,如何解决此问题

private void dgvacc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                
                int row = dgvacc.CurrentCell.RowIndex;
                int col = dgvacc.CurrentCell.ColumnIndex;

                try
                {
                    if (col < dgvacc.Columns.Count - 1)
                    {
                        dgvacc.CurrentCell = dgvacc.Rows[row -1].Cells[col + 1];
                        dgvacc.Focus();
                    }
                    else if (col == dgvacc.Columns.Count - 1)
                    {

                        dgvacc.CurrentCell = dgvacc.Rows[row].Cells[0];
                        dgvacc.Focus();
                    }
                }
                catch (Exception)
                {

                }
            }


        }
e1xvtsh3

e1xvtsh31#

备选案文1

覆盖窗体的ProcessCmdKey以捕获Keys.Enter按下并选择下一个单元格IF网格具有焦点ORDataGridView.EditingControl属性不是null/Nothing

public partial class YourForm : Form
{
    public YourForm()
    {
        InitializeComponent();
    }     

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((dataGridView1.Focused || dataGridView1.EditingControl != null)
            && keyData == Keys.Enter)
        {
            SelectNextCell();
            return true;
        }
        else
            return base.ProcessCmdKey(ref msg, keyData);
    }

    private void SelectNextCell()
    {
        var col = dataGridView1.CurrentCell.ColumnIndex;
        var row = dataGridView1.CurrentCell.RowIndex;

        if (col < dataGridView1.ColumnCount - 1) col++;
        else
        {
            col = 0;
            row++;
        }

        // Optional, select the first cell if the current cell is the last one.
        // You can return; here instead or add new row.
        if (row == dataGridView1.RowCount) row = 0;

        dataGridView1.CurrentCell = dataGridView1[col, row];
    }
}

备选案文2

DataGridView派生一个新类,并执行相同的操作,但 focusEditingControl 检查除外:

[DesignerCategory("Code")]
public class DataGridViewEx : DataGridView
{
    public DataGridViewEx() : base() { }

    // Optional to enable/disable this feature...
    public bool SelectNextCellOnEnter { get; set; } = true;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (SelectNextCellOnEnter && keyData == Keys.Enter)
        {
            SelectNextCell();
            return true;
        }
        else
        {
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }

    private void SelectNextCell()
    {
        var col = CurrentCell.ColumnIndex;
        var row = CurrentCell.RowIndex;

        if (col < ColumnCount - 1) col++;
        else
        {
            col = 0;
            row++;
        }

        if (row == RowCount) row = 0;

        CurrentCell = this[col, row];
    }
}
8tntrjer

8tntrjer2#

感谢所有我找到的答案,它解决了它-再次感谢

private DataGridViewCell dgvEndEditCell;
        private bool _EnterMoveNext = true;

        [System.ComponentModel.DefaultValue(true)]
        public bool OnEnterKeyMoveNext
        {
            get
            {
                return this._EnterMoveNext;
            }
            set
            {
                this._EnterMoveNext = value;
            }
        }

private void dgvacc_KeyDown(object sender, KeyEventArgs e)
        {
            

            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int iColumn = dgvacc.CurrentCell.ColumnIndex;
                int iRow = dgvacc.CurrentCell.RowIndex;
                if (iColumn == dgvacc.Columns.Count - 1 && iRow != dgvacc.Rows.Count - 1)
                {
                    dgvacc.CurrentCell = dgvacc[0, iRow + 1];
                }
                else if (iColumn == dgvacc.Columns.Count - 1 && iRow == dgvacc.Rows.Count - 1)
                {
                }
                else
                {
                    dgvacc.CurrentCell = dgvacc[iColumn + 1, iRow];
                }
            }

        }


private void dgvacc_SelectionChanged(object sender, EventArgs e)
        {
            if (this._EnterMoveNext && MouseButtons == 0)
            {
                if (this.dgvEndEditCell != null && dgvacc.CurrentCell != null)
                {
                    if (dgvacc.CurrentCell.RowIndex == this.dgvEndEditCell.RowIndex + 1
                        && dgvacc.CurrentCell.ColumnIndex == this.dgvEndEditCell.ColumnIndex)
                    {
                        int iColNew;
                        int iRowNew;
                        if (this.dgvEndEditCell.ColumnIndex >= dgvacc.ColumnCount - 1)
                        {
                            iColNew = 0;
                            iRowNew = dgvacc.CurrentCell.RowIndex;
                        }
                        else
                        {
                            iColNew = this.dgvEndEditCell.ColumnIndex + 1;
                            iRowNew = this.dgvEndEditCell.RowIndex;
                        }
                        dgvacc.CurrentCell = dgvacc[iColNew, iRowNew];
                    }
                }
                this.dgvEndEditCell = null;
            }

        }
ippsafx7

ippsafx73#

又一个迟来的回复。
有两种模式可以拦截ENTER键:
1.如果单元格处于编辑模式,则单元格本身必须处理键
1.否则,DGV必须处理该密钥
两者都是为了防止按ENTER键时细胞进展。
以下是“DataGridViewCustomColumn”的子类化解决方案,该解决方案的作用类似于“DataGridViewTextBoxColumn”,但在这两种情况下都禁止ENTER键前进。

public class DataGridViewCustomColumn : DataGridViewColumn
{
  public DataGridViewCustomColumn() : base(new DataGridViewCustomCell()) { }

  public class DataGridViewCustomCell : DataGridViewTextBoxCell
  {
    public override Type EditType => typeof(DataGridViewCustomEditingControl);

    protected override void OnDataGridViewChanged() {
      // register key down handler at DGV once
      var dgv = DataGridView;
      if (dgv != null && !dgvs.Contains(dgv)) {
        dgvs.Add(dgv);
        dgv.KeyDown += HandleKeyDown;
      }
    }

    private static HashSet<DataGridView> dgvs = new();

    private static void HandleKeyDown(object s, KeyEventArgs e) {
      if (s is not DataGridView dgv) return;
      if (dgv.CurrentCell is not DataGridViewCustomCell) return;

      if (e.KeyData == Keys.Enter) {
        //dont proceed to other cell
        if (e.KeyData == Keys.Enter) e.Handled = true;
      }
    }
  }

  public class DataGridViewCustomEditingControl : DataGridViewTextBoxEditingControl
  {
    protected DataGridView Dgv => (DataGridView)Parent?.Parent;

    protected override bool ProcessCmdKey(ref Message m, Keys keyData) {
      var dgv = EditingControlDataGridView;
      if (keyData == Keys.Enter && dgv.IsCurrentCellInEditMode) {
        // end edit but dont proceed to other cell
        dgv.EndEdit();
        dgv.CurrentCell.Selected = true;
        return true;
      }
      return base.ProcessCmdKey(ref m, keyData);
    }
  }
}

相关问题