Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView-when I press enter it goes to the next cell

i have a datagridview with 5 columns,when i press "enter" it goes to the next cell and when it arrives to the end of the rows when i press enter it adds a new rows,but my problem is when i move to the previous rows after i press enter it jumps the rows and does not go to the next cells,any help?

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];
            }
        }
    }
}
like image 626
user1264708 Avatar asked Mar 12 '12 20:03

user1264708


3 Answers

Forget about CellEnter event and the Form1_KeyPress event also. Just handle the dataGridView1_KeyDown event like this:

    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;
        }
    }

Please note that I changed the code a bit, and remember to set the Handled event property to true, otherwise it will process the default behavior.

Cheers!

like image 183
Reinaldo Avatar answered Oct 17 '22 01:10

Reinaldo


because the keyDown envent will not fire when press enter on datagridview cell this will be best thanks to @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);

}

like image 23
Hisham Avatar answered Oct 16 '22 23:10

Hisham


Try using this:

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);     
}

Just copy and paste the code.

Only thing you should have grid in your form.

like image 21
Nagarjun G N Avatar answered Oct 17 '22 01:10

Nagarjun G N