概要
UltraGrid には行をドラッグ アンド ドロップする機能があります。この記事は UltraGrid で行のドラッグ アンド ドロップを有効にする方法を紹介します。
手順
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;
// グリッドで行をドロップする位置を取得します
//行のグリッド座標 (ドロップ領域) を取得します
UIElement uieOver = ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(ultraGrid1.PointToClient(new Point(e.X, e.Y)));
//ドロップ領域の行 / ドラッグされた行をドロップする位置を取得します
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