文章分類/Infragistics
將 UltraGrid 的 AllowDrop 屬性設置為 True。
C#
ultraGrid1.AllowDrop = true;
VB.NET
UltraGrid1.AllowDrop = True
接下來,處理UltraGrid 的SelectionDrag 事件並調用UltraGrid 的DoDragDrop() 方法。當最終用戶選擇一行並開始拖動時,將觸發 SelectionDrag 事件。
private void ultraGrid1_SelectionDrag(object sender, CancelEventArgs e) { ultraGrid1.DoDragDrop(ultraGrid1.Selected.Rows, DragDropEffects.Move); }
Private Sub UltraGrid1_SelectionDrag(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles UltraGrid1.SelectionDrag UltraGrid1.DoDragDrop(UltraGrid1.Selected.Rows, DragDropEffects.Move) End Sub
處理 UltraGrid 的 DragOver 事件以設置拖動效果並啟用滾動。可以將行拖動到 UltraGrid 中的任何位置。
private void ultraGrid1_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; UltraGrid grid = sender as UltraGrid; Point pointInGridCoords = grid.PointToClient(new Point(e.X,e.Y)); if (pointInGridCoords.Y < 20) // 向上滖動 this.ultraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineUp); else if (pointInGridCoords.Y > grid.Height - 20) // 向下滖動 this.ultraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineDown); }
Private Sub UltraGrid1_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles UltraGrid1.DragOver e.Effect = DragDropEffects.Move Dim grid As UltraGrid = TryCast(sender, UltraGrid) Dim pointInGridCoords As Point = grid.PointToClient(New Point(e.X, e.Y)) If pointInGridCoords.Y < 20 Then '向上滖動 Me.UltraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineUp) ElseIf pointInGridCoords.Y > grid.Height - 20 Then '向下滖動 Me.UltraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineDown) End If End Sub
處理 UltraGrid 的 DragDrop 事件以獲取網格中的行放置位置。獲得位置後,您可以將行移到那裡。
private void ultraGrid1_DragDrop(object sender, DragEventArgs e) { int dropIndex; // 點擊後取得 Row 的位置 UIElement uieOver = ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(ultraGrid1.PointToClient(new Point(e.X, e.Y))); //取得 Drag Row 的位置 UltraGridRow ugrOver = uieOver.GetContext(typeof(UltraGridRow), true) as UltraGridRow; if (ugrOver != null) { dropIndex = ugrOver.Index; //網格中放置區域的索引/位置 //獲取拖拽的行 SelectedRowsCollection SelRows (SelectedRowsCollection)e.Data.GetData (typeof(SelectedRowsCollection)) as SelectedRowsCollection; //獲取選中的行數並將它們放入 dropIndex foreach (UltraGridRow aRow in SelRows) { //將選中的行移動到拖放區 ultraGrid1.Rows.Move(aRow, dropIndex); } } }
Private Sub UltraGrid1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles UltraGrid1.DragDrop Dim dropIndex As Integer '獲取行在網格中的放置位置 '獲取該行的網格坐標(拖放區域) Dim uieOver As UIElement = UltraGrid1.DisplayLayout.UIElement.ElementFromPoint(UltraGrid1.PointToClient(New Point(e.X, e.Y))) '獲取拖放區行/位置來拖放被拖行 Dim ugrOver As UltraGridRow = TryCast(uieOver.GetContext(GetType(UltraGridRow), True), UltraGridRow) If ugrOver IsNot Nothing Then dropIndex = ugrOver.Index '拖放區域在網格中的索引/位置 '獲取拖拽的行 Dim SelRows As SelectedRowsCollection = TryCast(DirectCast(e.Data.GetData(GetType(SelectedRowsCollection)), SelectedRowsCollection), SelectedRowsCollection) '獲取選中的行數,放入dropIndex For Each aRow As UltraGridRow In SelRows '將選中的行移動到拖放區 UltraGrid1.Rows.Move(aRow, dropIndex) Next End If End Sub