文章分類/

Infragistics|Ultimate UI Windows Form|如何在單個 UltraGrid 中 Drop & Drag Row

436 瀏覽人次
2023-04-08 更新

infragistics log

將 UltraGrid 的 AllowDrop 屬性設置為 True。

C#

ultraGrid1.AllowDrop = true; 

VB.NET

UltraGrid1.AllowDrop = True

接下來,處理UltraGrid 的SelectionDrag 事件並調用UltraGrid 的DoDragDrop() 方法。當最終用戶選擇一行並開始拖動時,將觸發 SelectionDrag 事件。

C#

private void ultraGrid1_SelectionDrag(object sender, CancelEventArgs e) { ultraGrid1.DoDragDrop(ultraGrid1.Selected.Rows, DragDropEffects.Move); } 

VB.NET

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 中的任何位置。

C#

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); } 

VB.NET

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 事件以獲取網格中的行放置位置。獲得位置後,您可以將行移到那裡。

C#

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);     } } } 

VB.NET

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 

快速跳轉目錄

✦ 群昱 AccessSoft 你的全面軟體解決方案 ✦

身為全球眾多知名軟體在台灣合作夥伴,歡迎諮詢你需要的軟體服務!

Picture of 軟體專家
軟體專家

群昱作為全球知名軟體推薦合作夥伴,致力於提供更多軟體解決方案給你!

更多軟體新知

立即詢價

請留下完整資訊,以便我們提供精確的服務內容給你。

詢價資訊