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

Signature
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
> 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
[quoted text clipped - 16 lines]
>
> Thanks
oskar@musiker.nu - 23 Jul 2007 17:00 GMT
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?
oskar@musiker.nu - 24 Jul 2007 07:32 GMT
On 23 Juli, 18:00, os...@musiker.nu wrote:
> I have created a form (with aDataGridView) that overrides
> ProcessKeyPreview as you show above. I have however encountered
[quoted text clipped - 7 lines]
> 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;
}
}