MAW74656 MAW74656 - 6 months ago 26
C# Question

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?


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.
        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.