Microsoft KB Archive/815252

= Form key events are not raised when you type in a DataGrid cell with the KeyPreview property set to true =

Article ID: 815252

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
When you set the KeyPreview property of the form to True, and you type in the cell of a DataGrid control, the Key events of the form (KeyUp, KeyDown) do not raise. However, when you press any key while focus is on the title, the column, or the lines of the DataGrid, the Key events of the form are raised as expected.



CAUSE
When the DataGrid cell has input focus, the Key events that were passed to the DataGrid control are first assigned to the DataGrid cell event handlers. The DataGrid cell does not support any Key events. The Key events are not enabled in the DataGrid to allow users to navigate inside the DataGrid control by using the keyboard. Therefore, Key events are not passed from the DataGrid cell to the parent Windows form, and the parent form Key event does raise.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create a new Windows application project by using Visual Basic .NET or Visual C# .NET.

By default Form1 is created. Right-click Form1, and then click Properties. In the Properties window, set the KeyPreview property to True . Drag a TextBox control and a DataGrid control to the form. Right-click DataGrid1, and then click Properties. Set the ReadOnly property to False .</li>  Add the following code in the Form1_KeyPress event handler:

Visual Basic .NET Code: MessageBox.Show(&quot;In Form1_ KeyPress Event Handler&quot;) Visual C# .NET Code: MessageBox.Show(&quot;In Form1_ KeyPress Event Handler&quot;); </li>  Add the following code in the TextBox1_KeyPress event handler:

Visual Basic .NET Code: MessageBox.Show(&quot;In TextBox1_ KeyPress Event Handler&quot;) Visual C# .NET Code: MessageBox.Show(&quot;In TextBox1_ KeyPress Event Handler&quot;); </li>  Add the following code in the DataGrid1_KeyPress event handler:

Visual Basic .NET Code: MessageBox.Show(&quot;In DataGrid1_ KeyPress Event Handler&quot;) Visual C# .NET Code: MessageBox.Show(&quot;In DataGrid1_ KeyPress Event Handler&quot;); </li> On the Data tab, double-click SqlDataAdapter, and then click Next.</li> On the Choose your data connection page, click New Connection.

The Data Link Properties window is displayed.</li> Type the database server name, username, and password, and then click to select the Northwind database. Click OK.</li> Click to select the data connection that you created in Step 12, and then click Next.</li> Under Choose a Query Type, click Next. Type Select * from customers on the Generate the SQL Statements page, and then click Finish.</li> Right-click SqlDataAdapter1, and then click Generate DataSet.

By Default, DataSet11 is created.</li> Click to select the DataGrid control, and then press the F4 key. In the Properties window, set the DataSource to the DataSet11.Customers that was generated in step 14.</li> On the Build menu, click Build Solution to build the project.</li> Run the application.</li> In the TextBox, type any text.

You receive the In Form1_ KeyPress Event Handler message and the In TextBox1_ KeyPress Event Handler message.</li> Click to select a cell in the DataGrid, and then type the text in the cell.

The message is not displayed.</li></ol>

<div class="references_section">