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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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
TheAvengerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.