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,574 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now