Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView "Enter" key event handling

I have a DataGridView populated with DataTable, have 10 columns. I have a scenario when moving from one row to another when I click on Enter key then I need that row should be selected and need to have that row values.

But here when I select n-th row then it automatically moves to n+1 Row.

Please help me in this...

In Page Load Event:

SqlConnection con = 
    new SqlConnection("Data Source=.;Initial Catalog=MHS;User ID=mhs_mt;Password=@mhsinc");

DataSet ds = new System.Data.DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from MT_INVENTORY_COUNT", con);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

Then,

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyChar == (Char)Keys.Enter)
     {
           int i = dataGridView1.CurrentRow.Index;
           MessageBox.Show(i.ToString());
     }     
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int i = dataGridView1.CurrentRow.Index;
    MessageBox.Show(i.ToString());
}
like image 547
MSanika Avatar asked Feb 19 '14 07:02

MSanika


3 Answers

That's the default behaviour of the DataGridView, and pretty standard in other data grids by 3rd party vendors too.

Here is what happens:

  1. The user hits the enter key
  2. The DataGridView receives the KeyPress event and performs various actions (such as ending edits, etc), and then moves the cell down one row.
  3. Then the DataGridView checks to see if there are any event handlers hooked up by you and fires those.

So by the time the enter key is pressed, the current cell has already changed.

You could use the following if you want to get the row that the user was on before the DataGridView changes the row. This should fit in with your existing code (obviously you will need to add the event handler for it):

void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        int i = dataGridView1.CurrentRow.Index;
        MessageBox.Show(i.ToString());
    }     
}

I hope that helps point you in the right direction. Not sure what you are hoping to do here, but hopefully this explains what you are seeing happen.

like image 69
Adam Valpied Avatar answered Oct 16 '22 07:10

Adam Valpied


Add a Class with the following code and modify your GridView designer page i.e.,

this.dataGridView1 = New System.Windows.Forms.DataGridView();

to

this.dataGridView1 = new GridSample_WinForms.customDataGridView();

the class file is:

class customDataGridView : DataGridView
{
  protected override bool ProcessDialogKey(Keys keyData)
  {
     if (keyData == Keys.Enter)
     {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
        this.CurrentCell = this[col, row];
        return true;
     }
     return base.ProcessDialogKey(keyData);
  }

  protected override void OnKeyDown(KeyEventArgs e)
  {
     if (e.KeyData == Keys.Enter)
     {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
        this.CurrentCell = this[col, row];
        e.Handled = true;
     }
     base.OnKeyDown(e);
  }
}
like image 4
MSanika Avatar answered Oct 16 '22 07:10

MSanika


The following solution is simpler and also works:

  1. In the .Designer.cs file:

    this.dataGridView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridView1_KeyDown);

  2. In the code behind file:

     private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
     {
         if (e.KeyData == Keys.Enter)
         {
             // Handle event
             e.Handled = true;
         }
     }
    
like image 4
Nata Avatar answered Oct 16 '22 05:10

Nata