Andrew
Andrew

Reputation: 933

How to change mouse cursor during drag and drop?

Background: I have a C# winforms application. I am dragging information from one datagridview to another. For my drag over event on the destination grid, I have the following code:

private void grid_DragOver(object sender, DragEventArgs e)
{
      if(e.Data.GetDataPresent(typeof(SelectedRecordsCollection)))
      {
          e.Effect = DragDropEffects.Move; 
      }
}

I want to limit the drop to only be allowed when the mouse is hovered over particular rows (say, rows with an odd index number). I currently limit what I actually add to the destination grid in the dragdrop event. However, because of the above code, my cursor changes to a Move icon as soon as the mouse hovers anywhere on the destination control.

Question: How do I make it so that the cursor is a "Cursor.No" icon everywhere on the destination grid, except set it to the Move icon for when the mouse is over a row with an odd index?

Thank you.

Edit: Aseem's solution ended up working for me.

Upvotes: 2

Views: 5786

Answers (1)

A G
A G

Reputation: 22577

Get the row index using HitTest. Try this, not tested though -

private void grid_DragOver(object sender, DragEventArgs e)
{
    // Get the row index of the item the mouse is below. 
    Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
    DataGridView.HitTestInfo hit = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
    if (hit.Type == DataGridViewHitTestType.Cell) {
        e.Effect = (hit.RowIndex%2 == 0)  //move when odd index, else none
            ? DragDropEffects.None
            : DragDropEffects.Move;
    }
}

Upvotes: 3

Related Questions