Get selected row when enter-key is pressed

Mar 26, 2009 at 11:29 AM
Hello,

When pressing the enter-key the datagrid changes the selection to the next row. There is the focus of my problem. I want to evaluate the value of the selected cell when the enter-key is pressed.
Which event can give me the right row or cell?
Coordinator
Mar 27, 2009 at 1:39 PM
If you mean you want to evaluate the value of a row/cell that is being edited and commited after an 'enter' key press, you can listen to the RowEditEnding event.  You can also use row validation (BindingGroup implementation for DataGrid), http://blogs.msdn.com/vinsibal/archive/2008/08/11/wpf-3-5-sp1-feature-bindinggroups-with-item-level-validation.aspx
Mar 27, 2009 at 3:31 PM
Hi vinsibal, thanks for your answer, but the occurrence of the problem is not in the case of editting a cell.

I have to select and extract the data of a "DataRow" when someone presses the enter-key. If I use the spacebar as the selector-key, the proplem not occures, because the selection stays on the same row. By using the enter-key, the selection changes to the next row and if I read the selectetItem-property of the DataGrid it returns the new selected row, which is the wrong one.

I have also testet the "keyDown"-event, but that way does not work, because the enter-Key does not fire a "keyDown"-event.


Coordinator
Mar 27, 2009 at 5:48 PM
So the functionality you want is:  When a user selects a row and presses enter, you want to do something with that row's data correct?

Have you tried the PreviewKeyDown event?
Mar 27, 2009 at 6:01 PM
Yes, correct, that's the solution for my problem.

Thanks a lot!