Skip to main content

Disable single click sort

Comments

7 comments

  • Dapfor Team

    Dear Deepak,

    The easiest way to implement this feature is to derive from the Grid and override OnMouseDown and OnDoubleClick methods as shown below:

     

    class MyGrid : Grid
    {
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if(HitTest(e.Location) != HitTestInfo.Column)
            {
    //Default routine
                base.OnMouseDown(e);
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if (HitTest(PointToClient(Cursor.Position)) != HitTestInfo.Column)
            {
                base.OnDoubleClick(e);
            }
            else
            {
    //Emulate mouse button down/up if the user has double clicked on the column
                Point pt = PointToClient(Cursor.Position);
                MouseEventArgs e2 = new MouseEventArgs(MouseButtons.Left, 1, pt.X, pt.Y, 0);
                base.OnMouseDown(e2);
                base.OnMouseUp(e2);
            }              
        }
    }

     

    Best regards,

    Dapfor

    0
  • Michael Sage

    Thanks,I had one more question. Is it possible to add items to the context menu that pops up when we right click on the column headers ? I am trying to add an option to do multiple column sort in that menu.

    0
  • Dapfor Team

    Yes, you can:

     

    grid.HeaderContextMenu.Opening += delegate(object sender, CancelEventArgs e)
    {
        grid.HeaderContextMenu.Items.Add("My item", null, delegate
        {
            //A code, when  the user clicks on this item
            MessageBox.Show(grid, string.Format("The user has clicked on MyItem"));
        });
    };

     

    Best regards,

    Dapfor

     

    0
  • Dapfor Team

    Don't forget to authorize mouse right button clicks on the header:

     

    class MyGrid : Grid
    {
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if ((HitTest(e.Location) != HitTestInfo.Column) || (e.Button == MouseButtons.Right))
            {
                base.OnMouseDown(e);
            }
        }
      
    //...
    }

     

    Regards,

    Dapfor

    0
  • Michael Sage

    Yes, I remember :-) . Thanks for your help.

    0
  • Michael Sage

    I just realized that we can't move columns once we disable the left click . Is  there a way to keep the column movement with the left click but not have the sort?

    Thanks.

    0
  • Dapfor Team

    Please find an improved example:

     

    class MyGrid : Grid
    {
        private Point _location;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            _location = e.Location;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (HitTest(e.Location) != HitTestInfo.Column || !Equals(_location, e.Location) || (e.Button == MouseButtons.Right))
            {
                //Default routine
                base.OnMouseUp(e);  
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if (HitTest(PointToClient(Cursor.Position)) != HitTestInfo.Column)
            {
                base.OnDoubleClick(e);
            }
            else
            {
                //Emulate mouse button down/up if the user has double clicked on the column
                Point pt = PointToClient(Cursor.Position);
                MouseEventArgs e2 = new MouseEventArgs(MouseButtons.Left, 1, pt.X, pt.Y, 0);
                base.OnMouseDown(e2);
                base.OnMouseUp(e2);
            }
        }
    }

     

    Regards,

    Dapfor

     

    0

Please sign in to leave a comment.

Powered by Zendesk