winforms 如何在发生按Enter键事件时将焦点移动到数据网格视图中的下一个单元格

0dxa2lsx  于 2023-03-13  发布在  其他
关注(0)|答案(6)|浏览(145)

朋友们,我正在使用C#开发Windows应用程序。我正在使用一个数据网格视图来显示记录。我需要的功能是当我按“Enter”键时,焦点应该转到下一个单元格(同一行的列)。如果它是网格中的最后一列,那么焦点应该转到下一行的第一列。我已经尝试使用

SendKeys.Send("{Tab}")

在datagridview1_KeyDown和datagridview1_KeyPress事件中。但是焦点正在向下对角移动。请帮助我解决这个问题。

uhry853o

uhry853o1#

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = true;
    int iColumn = dataGridView1.CurrentCell.ColumnIndex;
    int iRow = dataGridView1.CurrentCell.RowIndex;
    if (iColumn == dataGridView1.Columncount-1)
    {
        if (dataGridView1.RowCount > (iRow + 1))
        {
            dataGridView1.CurrentCell = dataGridView1[1, iRow + 1];
        }
        else
        {
            //focus next control
        }
    }
    else
        dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
}
wpx232ag

wpx232ag2#

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
    int icolumn = dataGridView1.CurrentCell.ColumnIndex;
    int irow = dataGridView1.CurrentCell.RowIndex;

    if (keyData == Keys.Enter)
    {                                
        if (icolumn == dataGridView1.Columns.Count - 1)
        {
            dataGridView1.Rows.Add();
            dataGridView1.CurrentCell = dataGridView1[0, irow + 1];
        }
        else
        {
            dataGridView1.CurrentCell = dataGridView1[icolumn + 1, irow];
        }
        return true;
    }
    else
        return base.ProcessCmdKey(ref msg, keyData);
    }
}
qmb5sa22

qmb5sa223#

可以使用数据网格视图的selectionchanged事件。在窗体中

private DataGridViewCell _celWasEndEdit;

        private void datagridview_SelectionChanged(object sender, EventArgs e)
    {

        if (MouseButtons != 0) return;

        if (_celWasEndEdit != null && datagridview.CurrentCell != null)
        {
            // if we are currently in the next line of last edit cell
            if (datagridview.CurrentCell.RowIndex == _celWasEndEdit.RowIndex + 1 &&
                datagridview.CurrentCell.ColumnIndex == _celWasEndEdit.ColumnIndex)
            {
                int iColNew;
                int iRowNew = 0;
                if (_celWasEndEdit.ColumnIndex >= datagridview.ColumnCount - 1)
                {
                    iColNew = 0;
                    iRowNew = dgvItems.CurrentCell.RowIndex;                   
                }
                else
                {
                        iColNew = _celWasEndEdit.ColumnIndex + 1;
                        iRowNew = _celWasEndEdit.RowIndex;
                }
                datagridview.CurrentCell = datagridview[iColNew, iRowNew];
            }
        }
        _celWasEndEdit = null;
      }

    private void datagridview_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        _celWasEndEdit = dgvItems[e.ColumnIndex, e.RowIndex];
    }
nhaq1z21

nhaq1z214#

bool notlastColumn =true; //class level variable--- to check either last column is reached or not

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.ColumnCount - 1 == e.ColumnIndex)  //if last column
    {
        KeyEventArgs forKeyDown = new KeyEventArgs(Keys.Enter);
        notlastColumn = false;
        dataGridView1_KeyDown(dataGridView1, forKeyDown);
    }
    else
    {
        SendKeys.Send("{up}");
        SendKeys.Send("{right}");
    }
}

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter && notlastColumn) //if not last column move to nex
    {
        SendKeys.Send("{up}");
        SendKeys.Send("{right}");
    }
    else if (e.KeyCode == Keys.Enter)
    {
        SendKeys.Send("{home}");//go to first column
        notlastColumn = true;
    }
}
q1qsirdb

q1qsirdb5#

即使我尝试的代码将焦点移到下一个单元格,但当单元格处于编辑模式时,它们会影响鼠标单击。这是我最后得出的结论。

bool _dgv_list_cellEndEdit = false; // class level variable

    void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            e.SuppressKeyPress=true;
            SendKeys.Send("{Tab}");
        }
    }

    void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        _dgv_list_cellEndEdit=true;
    }

    void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        _dgv_list_cellEndEdit=false;
    }

    void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if(_dgv_list_cellEndEdit)
        {
            _dgv_list_cellEndEdit=false;
            SendKeys.Send("{Up}");
            SendKeys.Send("{Tab}");
        }
    }
7gyucuyw

7gyucuyw6#

贴出了@Amritpal Singhs回答的一个稍微修改过的版本。
如果你有hidden(Visible=false)列作为你的网格的最后一列,那么@Amritpal Singhs的解决方案将抛出一个错误。下面的解决方案考虑了这些隐藏的列,并且没有例外:

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

   if (_lastCellEndEdit != null && gridDetails.CurrentCell != null)
   {
       // if we are currently in the next line of last edit cell
       if (gridDetails.CurrentCell.RowIndex == _lastCellEndEdit.RowIndex + 1 &&
           gridDetails.CurrentCell.ColumnIndex == _lastCellEndEdit.ColumnIndex)
       {
           int iColNew;
           int iRowNew = 0;

           var lastCellEndEditColumn = gridDetails.Columns[_lastCellEndEdit.ColumnIndex];
           if (lastCellEndEditColumn.DisplayIndex >= GetVisibleColumnsMaxDisplayIndex())
           {
               iColNew = GetFirstVisibleColumnIndex();
               iRowNew = gridDetails.CurrentCell.RowIndex;
           }
           else
           {
               iColNew = GetNextVisibleColumnIndex(lastCellEndEditColumn.DisplayIndex);
               iRowNew = _lastCellEndEdit.RowIndex;
           }
           gridDetails.CurrentCell = gridDetails[iColNew, iRowNew];
       }
   }
   _lastCellEndEdit = null;

}

private DataGridViewCell _lastCellEndEdit;

private void gridDetails_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
   _lastCellEndEdit = gridDetails[e.ColumnIndex, e.RowIndex];
}

int GetVisibleColumnsMaxDisplayIndex()
{
   int maxDisplayIndex = -1;
   foreach (DataGridViewColumn column in gridDetails.Columns)
   {
       if (column.Visible && column.DisplayIndex > maxDisplayIndex)
       {
           maxDisplayIndex = column.DisplayIndex ;
       }
   }
   return maxDisplayIndex;
}

int GetFirstVisibleColumnIndex()
{
   var firstVisibleColumnDisplayIndex = gridDetails.Columns.Count - 1;
   var firstVisibleColumnIndex = 0;
   foreach (DataGridViewColumn column in gridDetails.Columns)
   {
       if (column.Visible && column.DisplayIndex < firstVisibleColumnDisplayIndex)
       {
           firstVisibleColumnDisplayIndex = column.DisplayIndex;
           firstVisibleColumnIndex = column.Index;
       }
   }
   return firstVisibleColumnIndex;
}

int GetNextVisibleColumnIndex(int previousColumnDisplayIndex)
{
   var nextVisibleColumnDisplayIndex = gridDetails.Columns.Count;
   var nextVisibleColumnIndex = previousColumnDisplayIndex;

   foreach (DataGridViewColumn column in gridDetails.Columns)
   {
       if (column.Visible && column.DisplayIndex > previousColumnDisplayIndex 
           && (column.DisplayIndex - previousColumnDisplayIndex) <= (nextVisibleColumnDisplayIndex - previousColumnDisplayIndex))
       {
           nextVisibleColumnDisplayIndex = column.DisplayIndex;
           nextVisibleColumnIndex = column.Index;
       }
   }
   return nextVisibleColumnIndex;
}

相关问题