Solved

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

Posted on 2003-12-05
6
1,588 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:milenski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:bakobako
ID: 9885185
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
 
LVL 7

Expert Comment

by:TransBind
ID: 9885555
perhaps this will help.

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

0
 
LVL 6

Expert Comment

by:townsma
ID: 9886073
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:milenski
ID: 9887450
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
 

Expert Comment

by:2pac4ever
ID: 9891420
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
 
LVL 20

Accepted Solution

by:
TheAvenger earned 300 total points
ID: 9895168
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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question