简体   繁体   中英

How to disable Drag from a particular row of a DataGridView in C# and Winforms?

I'm looking for the best way to disable a row of a DataGridView after a successfull Drag&Drop action. So, I've already dropped the row and marked it as moved using background color.

Now I need to disable further Drag&Drop on the same row . Is this possible without removing the row?

I can de-select a dropped datagridview row but I can't mark it as not "draggable":

dgvToolPosition.Rows[dgvToolPosition.SelectedRows[0].Index].Selected = false;

Is there any possible way to set a property of the row avoiding the insertion of the corresponding index on a List<DataGridViewRowCollection> of already dropped rows?

Shot it the dark, but couldn't you just capture the drop event and check if you've moved it to the same row? I did a quick Google search for that event, this is what I came up with :

https://msdn.microsoft.com/en-us/library/system.windows.forms.dragdropeffects(v=vs.110).aspx

The best way for avoiding the Drag event on a previously dropped row is to check for the unique property of the row that I set on a successfull drop operation ( DefaultCellStyle.BackColor ):

// Check if a row in the current selection has a gray background color,
// so it can be skipped on Drag&Drop.
                foreach(DataGridViewRow selectedRow in dgvToolPosition.SelectedRows)
                {
                    if(selectedRow.DefaultCellStyle.BackColor == FCSCoreGlobal.ColorGray) 
                         return;
}

I know it's not the best way to do it, but until now it's the best trick I've found. I'm still looking for a AllowDrag property on a DataGridViewRow to set to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM