Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reject drag/drop based on object data?

I have c# DataGridViews modified so that I can drag and drop rows between them. I need to figure out how to either disable dragging on certain rows, or reject the drop for these rows. The criteria I'm using is a value in the datarow.

I'd like to disable the row (gray it out and not allow drag) as my first choice.

What options do I have? How can I disable or reject drag-drop based on criteria?

like image 336
MAW74656 Avatar asked Oct 06 '11 19:10

MAW74656


1 Answers

If you want to prevent a row from being dragged at all, use the following method instead:

void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
    DataGridViewRow row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow)); // Get the row that is being dragged.
    if (row.Cells[0].Value.ToString() == "no_drag") // Check the value of the row.
        e.Effect = DragDropEffects.None; // Prevent the drag.
    else
        e.Effect = DragDropEffects.Move; // Allow the drag.
}

Here, I presume you start the drag operation by doing something like this:

DoDragDrop(dataGridView1.SelectedRows[0], DragDropEffects.Move);

In this case, you don't need to use the method from my previous answer, of course.

like image 71
Maja Remic Avatar answered Oct 01 '22 03:10

Maja Remic