Selection of text inside cells of datagridview

rahul kumar 605 Reputation points
2023-11-07T10:01:48.9166667+00:00

Hi ,

how can we disable the text selection by the user when the cell is in edit mode inside a datagridview . I searched for the whole dastagridview but doesn't found anything .

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Accepted answer
  1. KOZ6.0 6,655 Reputation points
    2023-11-07T18:14:28.0066667+00:00

    Create a class that inherits from the NativeWindow class and disallows selection behavior.

    using System.Windows.Forms;
    
    class DisableSelectWindow : NativeWindow
    {
        private bool MouseIsDown = false;
    
        public DisableSelectWindow(TextBox owner) {
            AssignHandle(owner.Handle);
        }
    
        const int
            WM_KEYDOWN = 0x0100,
            WM_CHAR = 0x0102,
            WM_IME_CHAR = 0x0286,
            WM_SYSCHAR = 0x0106,
            WM_MOUSEMOVE = 0x0200,
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONDBLCLK = 0x0203,
            WM_LBUTTONUP = 0x0202,
            WM_CAPTURECHANGED = 0x0215,
            EM_SETSEL = 0x00B1;
    
        const char CTRL_A = '\x01';
    
        protected override void WndProc(ref Message m) {
            switch (m.Msg) {
                case WM_KEYDOWN:
                    Keys keyCode = (Keys)(int)(m.WParam.ToInt64() & 0xffff);
                    if (Control.ModifierKeys.HasFlag(Keys.Shift)) {
                        switch (keyCode) {
                            case Keys.Up:
                            case Keys.Down:
                            case Keys.Right:
                            case Keys.Left:
                                return;
                        }
                    }
                    base.WndProc(ref m);
                    break;
                case WM_CHAR:
                case WM_IME_CHAR:
                case WM_SYSCHAR:
                    char c = (char)(m.WParam.ToInt64() & 0xffff);
                    if (c == CTRL_A) {
                        return;
                    }
                    base.WndProc(ref m);
                    break;
                case WM_CAPTURECHANGED:
                    MouseIsDown = false;
                    base.WndProc(ref m);
                    break;
                case WM_LBUTTONDOWN:
                    MouseIsDown = true;
                    base.WndProc(ref m);
                    break;
                case WM_LBUTTONUP:
                    MouseIsDown = false;
                    base.WndProc(ref m);
                    break;
                case WM_LBUTTONDBLCLK:
                    break;
                case WM_MOUSEMOVE:
                    if (!MouseIsDown) {
                        base.WndProc(ref m);
                    }
                    break;
                case EM_SETSEL:
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
    }
    

    Use this like this:

    DisableSelectWindow disableSelectWindow;
    
    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) {
        TextBox textBox = e.Control as TextBox;
        if (textBox != null) {
            disableSelectWindow = new DisableSelectWindow(textBox);
        }
        base.OnEditingControlShowing(e);
    }
    
    protected override void OnCellEndEdit(DataGridViewCellEventArgs e) {
        if (disableSelectWindow != null) {
            disableSelectWindow.ReleaseHandle();
            disableSelectWindow = null;
        }
        base.OnCellEndEdit(e);
    }
    
    

    It feels strange, what is the purpose?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.