how to capture a keyPress event on a read-only dataGrid that's been given the focus (or so I assume ?!)

I have a read-only datagrid that I pass the focus with
DataGridName.Focus();
One thing that I am confused about is that when the above is called, the first cell in the dataGrid is being given the focus and when I try to caputre the keyPress event, nothing happens. I assumed that may be the above call automatically gives focus to the first DataGridCell, but when I cheked the documentation, there were no events for the DataGridCell control. I am completely lost here.

In few words, what I am trying to do is the following:
I have a textBox for which depending on the character entered I give focus to a particular DataGrid. Then I navigate through the rows and on Enter key press I want to transfer the focus back to the textBox and at the same time grab the value of the first field in the selected row. So far I wasn't able to figure out the mystery with the DataGrid Focus and keyPress event.
milenskiAsked:
Who is Participating?
 
TheAvengerConnect With a Mentor Commented:
The problem is that when you select a cell in the DataGrid, the grid shows a TextBox, shows the text of the cell inside it and gives it the focus. Then the text box handles the keypress events and they don't reach the datagrid. When you select the grid itself (by clicking the border) then it gets the events.

The solution: for every column in your table there is a DataGridColumnStyle. You can create these manually or they are created for you by the framework. What you need to do is take the DataGridColumnStyle for the selected column and get its TextBox. Assuming that you did not put your own column styles, the style of your column is always DataGridTextBoxColumn and it has a property TextBox. If you created the DataGridColumnStyles yourself, you will know what type they are.

To get the column styles use:

dataGrid.TableStyles[0][columnNumber]

So for default column styles:

((DataGridTextBoxColumn)dataGrid.TableStyles[0][columnNumber]).TextBox.KeyPress += new EventHandler (....);
0
 
bakobakoCommented:
Can you clarify this? Where are you having the difficulty? Is it in getting the cell information from the selected row or is it that when the key press occurs the row is changed?
0
 
TransBindCommented:
perhaps this will help.

((DataGridTextBox)this.colColumnName.TextBox).TextChanged += new
System.EventHandler(this.EventHandlerFunctionName);

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
townsmaCommented:
The events are captured by the Datagid itself, not the individual cells.  If you create an event handler on the grid for the keypress, this will detect what button has been pressed. You can then place code to see if it is an Enter key, then you can use the Datagris.CurrentCell to obtain the current value of the cell you are over.


      // Create event handler
      this.dataGrid1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dataGrid1_KeyPress);


       private void dataGrid1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
       {
      if(e.KeyChar == 13)
      {
              // now extract the cell value using the datagrid.CurrentCell  

      }
       }


Good luck, hope this helps.


0
 
milenskiAuthor Commented:
The problem is the fact that when I do DataGridName.Focus(), the focus is given not to the DataGrid itself but the first cell of the it, so when I try to capture a keyPress event for the DataGrid, naturally, there is no response and at the same time I don't know how to capture keypress events in a DataGridCell(if this actually is the control with the focus), especially when the DataGrid is read-only, which  means that even if there was TextChanged event on a DataGridCell, it still won't do the work.

I did a test to check if the keyPress event on DataGrid works, but I had to manually select the outer border of the DataGrid, and after that I got the response specified in the eventHandling function. So seems like, after all, that the Focus method of the DataGrid doesn't give the focus to the DataGrid control itself, but to another control within it(may be the current DataGridCell), that I don't know how to capture the keyPress event for.
0
 
2pac4everCommented:
if the keypress event does not exist for a certain type of control or u just dont want to use it, a windows hook is a very popular option.

u need to import these functions from user32.dll (assuming that u r running a 32-bit windows platform)

HHOOK SetWindowsHookEx(
    int idHook,
    HOOKPROC lpfn,
    HINSTANCE hMod,
    DWORD dwThreadId
);

LRESULT CallNextHookEx(
    HHOOK hhk,
    int nCode,
    WPARAM wParam,
    LPARAM lParam
);

BOOL UnhookWindowsHookEx(          
    HHOOK hhk
);

if u want to capture all keys pressed, not just the ones intended for your app then u can use a global hook

hope this helps... suma
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.