6

在 DataGridView 中,按 SHIFT 和 SPACE 将默认选择整行。我找到的唯一解决方案(在vb.net DataGridView - Replace Shortcut Key with typed character中引用)是关闭行选择功能。虽然这可行,但并不理想,因为我仍然希望能够使用行选择器选择整行(例如,删除行),并将SelectionMode属性更改为除RowHeaderSelect我失去该能力之外的任何其他内容。有没有办法只捕获 SHIFT+SPACE 组合并用简单的 SPACE 替换它?MutiSelect当控件的属性设置为True并且SelectionMode属性设置为时,似乎没有任何键事件甚至可以识别击键RowHeaderSelect,所以我不能使用这些。

ETA:我想也许关闭MultiSelect并将选择模式更改为CellSelect,然后为该RowHeaderMouseClick事件添加一个事件处理程序会起作用......不。

4

3 回答 3

2

我想出如何做到这一点的最好方法是从 DataGridView 继承并覆盖 ProcessCmdKey 方法。然后你可以拦截 Shift+Space 并在 Space 上发送。只需将此类添加到您的项目并将所有 DataGridViews 切换到 MyDataGridViews。我的解决方案从这个DataGridView keydown event not working in C# SO question(这也解释了为什么 Zaggler 的解决方案不起作用)和ProcessCmdKey 中的这个 SendKeys 中汲取灵感:将 Shift-Space 更改为 Space Bytes.com 帖子。对不起,但它在 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);
    }
}
于 2016-08-10T19:54:24.637 回答
1

这是我的解决方案:

private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
}

private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
}
于 2020-12-28T19:51:18.970 回答
0

在这里,这对我来说很好......

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

这是另一种方式..

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

只是对它们进行试验,我希望对你有用吗?

于 2013-01-10T08:27:48.800 回答