?
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
Medium Priority
?
1,595 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
Industry Leaders: 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 900 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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