繁体   English   中英

移至Datagridview Winforms c#中Enter键上的下一个单元格

[英]Move to next cell on Enter key in Datagridview Winforms c#

我有一个DataGridView ,每当按下ENTER键时,我就会从一个单元格移动到另一个单元格。

我设法找到了当单元处于编辑模式而不是处于编辑模式时的解决方案。

我面临的问题是,当用户在最后一行,当他正在编辑一个单元格,然后按Enter时,一个事件没有触发( selectionChanged ),并且该单元格没有保持焦点。 它只是退出编辑模式。 这很奇怪,因为它仅发生在最后一行。 所有其他行均按预期工作。

我正在使用2个事件,以便在单元格处于编辑模式时移至Enter键上的下一个单元格。

private void dgvTT_SelectionChanged(object sender, EventArgs e)
    {
        if (MouseButtons != 0) return;

        if (_celWasEndEdit != null && dgvTT.CurrentCell != null)
        {
            // if we are currently in the next line of last edit cell
            if (dgvTT.CurrentCell.RowIndex == _celWasEndEdit.RowIndex + 1 &&
                dgvTT.CurrentCell.ColumnIndex == _celWasEndEdit.ColumnIndex)
            {
                int iColNew;
                int iRowNew = 0;
                if (_celWasEndEdit.ColumnIndex >= colMaam.Index)
                {
                    iColNew = colMisparHeshbon.Index;
                    iRowNew = dgvTT.CurrentCell.RowIndex;
                }
                else
                {
                    iColNew = dgvTT.Columns.GetNextColumn(_celWasEndEdit.OwningColumn, DataGridViewElementStates.Displayed, DataGridViewElementStates.None).Index;  
                    iRowNew = _celWasEndEdit.RowIndex;
                }
                dgvTT.CurrentCell = dgvTT[iColNew, iRowNew];
            }
        }
        _celWasEndEdit = null;
    }

    private void dgvTT_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        _celWasEndEdit = dgvTT[e.ColumnIndex, e.RowIndex];
    }

有什么建议么?

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;
                int iRow = dataGridView1.CurrentCell.RowIndex;
                if (iColumn == dataGridView1.Columns.Count - 1)
                    dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
                else
                    dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
            }
            } 
            catch { }
        }
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        { 
            if (e.ColumnIndex == dataGridView1.Columns.Count - 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
            }
            else
            {
                SendKeys.Send("{UP}");
                SendKeys.Send("{left}");
            }

        }
 private void button1_Click(object sender, EventArgs e)
    {
        int indexrow = dgv.BindingContext. Position;
        int iColNew;
        int iRowNew = 0;
        iColNew = colMisparHeshbon.Index;
        dgv.Rows[dgv.CurrentRow.Index + 1].Selected = true;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM