Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:12:58+00:00 2026-05-27T02:12:58+00:00

I have a DataGridView, where I allow users to Drag/Drop rows to reorder them.

  • 0

I have a DataGridView, where I allow users to Drag/Drop rows to reorder them. This works fine, but it seems to be very sensitive – just clicking and moving the mouse slightly when clicking a cell starts the drag/drop operation.

So what I want to do is only allow dragging and dropping from the ‘side bar’ of the DataGridView – i.e. the ‘column’ to the left of the red line in the below image:

enter image description here

Is this possible? Here is my code:

[code]

    private Rectangle dragBoxFromMouseDown;
    private int rowIndexFromMouseDown;
    private int rowIndexOfItemUnderMouseToDrop;

    private void grdCons_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {
                DragDropEffects dropEffect = grdCons.DoDragDrop(grdCons.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
            }
        }
    }

    private void grdCons_MouseDown(object sender, MouseEventArgs e)
    {
        rowIndexFromMouseDown = grdCons.HitTest(e.X, e.Y).RowIndex;
        if (rowIndexFromMouseDown != -1)
        {
            Size dragSize = SystemInformation.DragSize;
            dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
        }
        else
        {
            dragBoxFromMouseDown = Rectangle.Empty;
        }
    }

    private void grdCons_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void grdCons_DragDrop(object sender, DragEventArgs e)
    {
        Point clientPoint = grdCons.PointToClient(new Point(e.X, e.Y));
        rowIndexOfItemUnderMouseToDrop = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex;

        if (e.Effect == DragDropEffects.Move)
        {
            DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
            grdCons.Rows.RemoveAt(rowIndexFromMouseDown);
            grdCons.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
        }
    }

[/code]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:12:58+00:00Added an answer on May 27, 2026 at 2:12 am

    I managed to get Drag-and-Drop using only row headers working by checking the DataGridViewHitTestType in the MouseDown event handler like so:

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        // Get the index of the item the mouse is below.
        rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
    
        // Here we also check that the HitTest happened on a RowHeader
        if (rowIndexFromMouseDown != -1 && (dataGridView1.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.RowHeader))
        {
            // Remember the point where the mouse down occurred. 
         // The DragSize indicates the size that the mouse can move 
         // before a drag event should be started.                
            Size dragSize = SystemInformation.DragSize;
    
            // Create a rectangle using the DragSize, with the mouse position being
            // at the center of the rectangle.
            dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                                                           e.Y - (dragSize.Height / 2)),
                                dragSize);
        }
        else
            // Reset the rectangle if the mouse is not over an item in the ListBox.
            dragBoxFromMouseDown = Rectangle.Empty;
    }
    

    I’m guessing that you got your code from the DataGridView FAQ since the code you have is almost identical to the example there?

    If you didn’t then I’d recommend having a look at the FAQ – it is filled with lots of good advice and examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app with 2 DataGridView's and have implemented Drag/Drop to allow the
I have a DataGridView binded to this table: [Users] ID Name -- ---- 11
I'm not sure if this is the right approach but I have a datagridview
I have a datagridview that is using paging, it works perfectly fine and I
I have c# DataGridViews modified so that I can drag and drop rows between
I have a datagridview where the users can select which subset of columns to
So I have this datagridview that is linked to a Binding source that is
I have a binded DataGridView that allows adding new rows. The problem is that
I have a DataGridView that is bound to a DataSet and I allow someone
I have a datagridview in a C# Winform project. This datagridview is used to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.