How to handle the DataGrid KeyUp events?

G

Guest

I’m using a DataGrid control on my form; and I want to handle the KeyUp event.
So I did:
myDataGrid.KeyUp += new KeyEventHandler(this.OnKeyUp);

When the DataGrid is empty, I mean when It has no data so now rows are seen,
all keys are causing my this.OnKeyUp(...) to be invoked correctly.

But when the DataGrid is populated with some data (rows are seen with only
DataGridTextBoxes), only several keys are causing it to be invoked.

I assumed that the DataGridTextBox which were added are stealing the KeyUp
events. So I also did:
myDataGrid.ControlAdded += new ControlEventHandler(this.OnControlAdded);
private void OnControlAdded(object sender, ControlEventArgs e)
{
e.Control.KeyUp += new KeyEventHandler(this.OnKeyUp);
}

But still the this.OnKeyUp(...) is not invoked.

I noticed that the myDataGrid.Controls.Count value is greater then the
number of times my OnControlAdded(...) is invoked. So for every control in
myDataGrid.Controls I also did:
foreach( Control ctrl in m_dataGrid.Controls )
{
ctrl.KeyUp += new KeyEventHandler(this.OnKeyUp);
}


But still the results are the same.


Can anybody tell me how can I get and handle the keys events (KeyUp,
KeyDown, ...) of my DataGrid?
 
G

Guest

Ok, I found the problem.

The DataGridTextBox do steal the events because they also steal the focus.
So I did:
(1) myDataGrid.KeyUp += new KeyEventHandler(this.OnKeyUp);
(2) myDataGrid.ControlAdded += new ControlEventHandler(this.OnControlAdded);
(3) private void OnControlAdded(object sender, ControlEventArgs e)
{
e.Control.Enter += new EventHandler(this.OnEnter);
}
(4) void OnEnter (object sender, EventArgs e)
{
If( myDataGrid.Focus = false )
{
myDataGrid.Focus();
}
}

And now my this.OnKeyUp(...) handler is not correctly.
 
G

Guest

Sorry, I have an error in the text I poster in the last line, instead:

And now my this.OnKeyUp(...) handler is invoked correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top