DataGridView - 当我按下输入它进入下一个单元格

时间:2012-03-12 20:10:51

标签: c# winforms ado.net

我有一个5列的datagridview,当我按下“enter”它进入下一个单元格,当它按到Enter键到达行的末尾时,它会添加一个新的行,但我的问题是当我移动按下输入后的前一行,它会跳转行并不会转到下一个单元格,任何帮助?

public partial class Form1 : Form
{
    public static int Col;
    public static int Row;

    public Form1()
    {
        InitializeComponent();
    }       

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.Rows.Add();           
    }   

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {           
        Col = dataGridView1.CurrentCellAddress.X;        
        Row = dataGridView1.CurrentCellAddress.Y;     
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {            
        if (e.KeyChar == (int)Keys.Enter)
        {              
            if (Col + 1 < 5)
            {
                dataGridView1.CurrentCell = dataGridView1[Col + 1, Row];
            }
            else
            {                        
                dataGridView1.Rows.Add();
                dataGridView1.CurrentCell = dataGridView1[Col - 4, Row + 1];
            }
        }
    }
}

8 个答案:

答案 0 :(得分:11)

忘记CellEnter事件和Form1_KeyPress事件。只需处理 dataGridView1_KeyDown 事件,如下所示:

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            int col = dataGridView1.CurrentCell.ColumnIndex;
            int row = dataGridView1.CurrentCell.RowIndex;

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

            if (row == dataGridView1.RowCount)
                dataGridView1.Rows.Add();

            dataGridView1.CurrentCell = dataGridView1[col, row];
            e.Handled = true;
        }
    }

请注意我稍微更改了代码,并记得将Handled事件属性设置为true,否则它将处理默认行为。

干杯!

答案 1 :(得分:4)

这对我有用

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {                
            e.SuppressKeyPress = true;
            int row = dataGridView1.CurrentRow.Index;
            int col = dataGridView1.CurrentCell.ColumnIndex;
        }
    }

答案 2 :(得分:2)

尝试使用:

bool notlastColumn = true;

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;
    int i = irow;
    if (keyData == Keys.Enter)
    {
        if (icolumn == dataGridView1.Columns.Count - 1)
        {
             //dataGridView1.Rows.Add();
            if (notlastColumn == true)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
            }
            dataGridView1.CurrentCell = dataGridView1[0, irow + 1];
         }
        else
        {
            dataGridView1.CurrentCell = dataGridView1[icolumn + 1, irow];
        }
        return true;
    }
    else
        if (keyData == Keys.Escape)
        {
            this.Close();
            return true;
        }
    //below is for escape key return
    return base.ProcessCmdKey(ref msg, keyData);
    //below is for enter key return 
    return base.ProcessCmdKey(ref msg, keyData);     
}

只需复制并粘贴代码即可。

只有你的表格中应该有网格。

答案 3 :(得分:0)

你可以做的是处理KeyDown事件以检查是否按下了返回键,你取消事件并告诉应用程序在datagridview中使用所选的+1索引:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyData == Keys.Enter)
   {
      int column = dataGridView1.CurrentCell.ColumnIndex;
      int row = dataGridView1.CurrentCell.RowIndex;
      dataGridView1.CurrentCell = dataGridView1[column, row+1];
      e.Handled=true;
   }
}

为防止出现问题,您还应插入一个子句,以检查用户按下返回后是否有可用的项目。

答案 4 :(得分:0)

使用此解决方案:

private void Form1_Load(object sender, EventArgs e)
    {
        dtg.AllowUserToAddRows = false;
    }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        int iCol = dtg.CurrentCell.ColumnIndex;
        int iRow = dtg.CurrentCell.RowIndex;

        if (keyData == Keys.Enter)
        {
            if (iCol == dtg.ColumnCount - 1)
            {
                if (iRow + 1 == dtg.RowCount)
                {
                    dtg.Rows.Add();
                }

                dtg.CurrentCell = dtg[0, iRow + 1];
            }
            else
            {
                dtg.CurrentCell = dtg[iCol + 1, iRow];
            }
            return true;
        }
        else if (keyData == Keys.Escape)
        {
            Close();
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

祝你好运......

答案 5 :(得分:0)

因为按下在datagridview单元格上输入时,keyDown envent不会触发,这要归功于@Nagarjun

bool notlastColumn = true;

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;
int i = irow;
if (keyData == Keys.Enter)
{
    if (icolumn == dataGridView1.Columns.Count - 1)
    {
         //dataGridView1.Rows.Add();
        if (notlastColumn == true)
        {
            dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
        }
        dataGridView1.CurrentCell = dataGridView1[0, irow + 1];
     }
    else
    {
        // to pass hidden cell, because will fire exception
        //exception: Current cell cannot be set to an invisible cell
        // the do while loop will enable you to pass any hidden cell
         do
          {
            icolumn++;
          } while (!dgv[icolumn, irow].Visible);
        dataGridView1.CurrentCell = dataGridView1[icolumn, irow];
    }
    return true;
}
else
    if (keyData == Keys.Escape)
    {
        this.Close();
        return true;
    }
//below is for escape key return
return base.ProcessCmdKey(ref msg, keyData);

}

答案 6 :(得分:0)

当按键进入时,只需向右移动......

private void datagridview_KeyDown(object sender, KeyEventArgs e)        
{   
     if (e.KeyCode == Keys.Enter)
     {
          SendKeys.Send("{Right}");
     }
}

答案 7 :(得分:0)

这段代码对我来说很好

key