Override DataGridView Shift + Space

In a DataGridView, pressing SHIFT and SPACE by default will select the entire row. The only solution I found (link to vb.net DataGridView - Replace shortcut with typed character ) is to disable the row select function. Although this works, it is not ideal, because I would still like to select the whole row using the row selector (for example, to delete the row) and changing the SelectionMode property to anything other than RowHeaderSelect I lose this ability. Is there a way to catch only the SHIFT + SPACE combination and replace it with plain space? It seems that none of the key events even recognize that a keystroke when the MutiSelect control MutiSelect set to True and the SelectionMode property is set to RowHeaderSelect , so I cannot use them.

ETA: I thought maybe turning off MultiSelect and changing the selection mode to CellSelect , then adding an event handler for the RowHeaderMouseClick event would work ... nope.

+4
source share
2 answers

Here it works great for me ....

 Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown 'Lets see what keys we have down shall we?' If My.Computer.Keyboard.ShiftKeyDown And e.KeyCode = Keys.Space Then DataGridView1.CurrentCell.Selected = False End If End Sub 

Here is another way.

 Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown 'Lets see what keys we have down shall we?' If My.Computer.Keyboard.ShiftKeyDown And e.KeyCode = Keys.Space Then 'SendKeys.Send(Keys.Space) DataGridView1.CurrentCell.Selected = False End If End Sub 

Just experiment with them, and I hope something works for you?

0
source

The best way I figured out how to do this is to inherit from the DataGridView and override the ProcessCmdKey method. Then you can intercept Shift + Space and just send to Space. Just add this class to your project and switch all your DataGridViews to MyDataGridViews. My solution draws inspiration from this Keydown KeyGridView event not working in C # . The SO question (which also explains why the Zaggler solution does not work) and this is the SendKeys in ProcessCmdKey: change Shift-Space to just Post Bytes.com space . Sorry, but in C #.

 class MyDataGridView : System.Windows.Forms.DataGridView { protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) { if (keyData == (System.Windows.Forms.Keys.Space | System.Windows.Forms.Keys.Shift)) { // DataGridView is dumb and will select a row when the user types Shift+Space // if you have the DGV set so that you can click a row header to select a row (for example, to delete the row) // this method will intercept Shift+Space and just send on Space so that the DGV properly handles this. // For example, if I type "ME TYPING IN ALL CAPS" it ends up looking like "METYPINGINALLCAPS". // Or if I type "Note: I have some OS thing to talk about" it looks like "Note:Ihave some OSthing to talk about" byte[] keyStates = new byte[255]; UnsafeNativeMethods.GetKeyboardState(keyStates); byte shiftKeyState = keyStates[16]; keyStates[16] = 0; // turn off the shift key UnsafeNativeMethods.SetKeyboardState(keyStates); System.Windows.Forms.SendKeys.SendWait(" "); keyStates[16] = shiftKeyState; // turn the shift key back on UnsafeNativeMethods.SetKeyboardState(keyStates); return true; } return base.ProcessCmdKey(ref msg, keyData); } [System.Security.SuppressUnmanagedCodeSecurity] internal static class UnsafeNativeMethods { [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern int GetKeyboardState(byte[] keystate); [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern int SetKeyboardState(byte[] keystate); } } 
0
source

All Articles