2010-08-09 101 views
1

我有一個datagridview表,我想在某些特定情況下禁用拖放操作。c#控制datagridview列的拖放操作

實施例:

我有4列命名爲A,B,C,d。我將它們分成兩組:A,B和C,D(對於一個用戶,它們看起來像4個普通列)。現在,當用戶拖動列A或B時,他只能拖放它在C之前。當拖動C或D時,他只能在B後拖放它。

我試圖檢測ColumnDisplayIndexChanged中的DisplayIndex值,但此事件被多次觸發,並且我得到了DisplayIndex正在被改變或者某種情況,我不能在那個時候改變它。

有沒有辦法做到這一點?
在此先感謝。

回答

0

拖放以DataGridView::MouseDown事件開始。在那裏,你能記住這列被拖到

// Get the index of the item the mouse is below. 
int columnIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).ColumnIndex; 
// Get the name 
string columnDragName = dataGridView1.Columns[columnIndexFromMouseDown].Name; 

記住的列名,並檢查它反對DataGridView::DragDrop的「下拉列」。如果它可以被丟棄,那麼做你的魔法,否則如果列組合不正確,什麼都不做。