Reject drag and 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 disable the drag and drop of certain rows or reject the fall for these rows. The criteria I use is the value in datarow.

I would like to disable the line (gray it out and not allow drag and drop) as my first choice.

What are my options? How to disable or reject drag and drop by criteria?

+5
source share
3 answers

If you want to prevent the line 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.
}

, , - :

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

, , .

+4

, :

    void dataGridView1_DragOver(object sender, DragEventArgs e)
    {
        Point cp = PointToClient(new Point(e.X, e.Y)); // Get coordinates of the mouse relative to the datagridview.
        var dropped = dataGridView1.HitTest(cp.X, cp.Y); // Get the item under the mouse pointer.
        if (dataGridView1.Rows[dropped.RowIndex].Cells[0].Value.ToString() == "not_allowed") // Check the value.
            e.Effect = DragDropEffects.None; // Indicates dragging onto this item is not allowed.
        else
            e.Effect = DragDropEffects.Move; // Set the drag effect as required.
    }

, , :

dataGridView1.DragOver += new DragEventHandler(dataGridView1_DragOver);

if . , "not_allowed".

+2

All Articles