根据对象数据拒绝拖放?

MAW*_*656 5 c# user-interface drag-and-drop winforms

我修改了c#DataGridViews,以便我可以在它们之间拖放行.我需要弄清楚如何禁用某些行的拖动,或拒绝这些行的拖放.我正在使用的标准是数据行中的值.

我想禁用该行(灰色,不允许拖动)作为我的第一选择.

我有什么选择?如何根据条件禁用或拒绝拖放?

Maj*_*mic 5

如果要防止拖动行,请改用以下方法:

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.
}
Run Code Online (Sandbox Code Playgroud)

在这里,我假设您通过执行以下操作来启动拖动操作:

DoDragDrop(dataGridView1.SelectedRows[0], DragDropEffects.Move);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您当然不需要使用我之前的答案中的方法.