DataGridView and Enter key

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a win form having DataGridView and one button which is accept button
for this form. Now when the focus is on form, enter key causes click event of
accept to fire. But when the focus is on DataGridView, enter key does not
fire click event of accept button.

I want click event to fire when focus is on DataGridView and user presses
enter key.

Steps to reproduce:

1) In WinForm application, add DataGridView and Button
2) Set added button as accept button and add click event for this button
3) Now run the application and click the DataGridView. The focus is on
DataGridView and pressing enter key, click event of button does not fire.
4) Run the application and set focus to form. Pressing enter key fires click
event of button.

Anyone knows how to solve this problem?

Thanks
 
Hi,
This behaviour is by design.The workaround for this is to override method
ProcessKeyPreview.Find below sample code which solves ur problem:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

protected override bool ProcessKeyPreview(ref
System.Windows.Forms.Message m)
{

Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;

if (keyCode == Keys.Enter)
{
SomeMethod();
}

return true;
}

private void button1_Click(object sender, EventArgs e)
{
SomeMethod();
}
private void SomeMethod()
{
MessageBox.Show("Button1 Clicked");
}

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
col1.DataType = typeof(System.String);
col1.ColumnName = "Name";
dt.Columns.Add(col1);

DataRow row1 = dt.NewRow();
row1["Name"] = "Manish Bafna";

dt.Rows.Add(row1);

dataGridView1.DataSource = dt;
}

}
}
 
I have created a form (with a DataGridView) that overrides
ProcessKeyPreview as you show above. I have however encountered
another problem. The form with the DataGridView is shown when a button
is clicked on another form. If this button is clicked using the enter
key, the enter event is passed on to the form with the DataGridView
and the enter event is processed by this form too, causing an
undesireable result.

So, the question is: Can the code in ProcessKeyPreview in your example
be modified to handle this case, or should the form with the button
contain some code that handles this?
 
I have created a form (with aDataGridView) that overrides
ProcessKeyPreview as you show above. I have however encountered
another problem. The form with theDataGridViewis shown when a button
is clicked on another form. If this button is clicked using theenter
key, theenterevent is passed on to the form with theDataGridView
and theenterevent is processed by this form too, causing an
undesireable result.

So, the question is: Can the code in ProcessKeyPreview in your example
be modified to handle this case, or should the form with the button
contain some code that handles this?

I solved this myself by using the following instead of overriding
ProcessKeyPreview:

private void dataGridView_KeyDown(object sender, KeyEventArgs args)
{
if (args.KeyCode == Keys.Enter)
{
SomeMethod();
args.Handled = true;
}
}
 
Back
Top