2010-08-09 36 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的“下拉列”。如果它可以被丢弃,那么做你的魔法,否则如果列组合不正确,什么都不做。