Main Menu
Knowledge Base
Product Registration
Log an Incident
Request a Feature
Search Incidents/Bug Reports


Search KB

Please note: In an effort to better serve you, we are in the process of restructuring DevCenter. In the process, we have moved many items that you may be used to finding in DevCenter over to the Main Site. If you are having trouble locating something, please try looking at the following places:

Knowledge Base Article: KB010086

HOWTO:How to drag and drop rows in a single WinGrid


The information in this article applies to:
UltraWinGrid (v6.3.20063, 7.1.20071, 7.2.20072, 7.3.20073)
  Article Created: 
11/2/2007

Last Updated:
11/2/2007

Article Type
How To
  
Page Options
Average Rating:
8 out of 10

Rate this page
Print this page
E-mail this page
Add to Favorites

Summary

The UltraGrid has the ability to allow the end-user to drag and drop rows to a different location within it. This article demonstrates how to allow rows to be dragged to different locations in an UltraGrid.

Step-By-Step Example

The first step is to set the UltraGrid’s AllowDrop property to True.
In C#:

ultraGrid1.AllowDrop = true;

In VB.Net:

UltraGrid1.AllowDrop = True

Next, handle the UltraGrid’s SelectionDrag event and call the UltraGrid’s DoDragDrop() method. The SelectionDrag event fires when the end-user clicks and starts to drag a selected row.

In C#:

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

In 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

Now handle the UltraGrid’s DragOver event to set the drag effects and permit scrolling to occur so you can drag the row anywhere on the UltraGrid.
In 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)
        // Scroll up.
        this.ultraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineUp);
    else if (pointInGridCoords.Y > grid.Height - 20)
        // Scroll down.
        this.ultraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineDown);
}

In 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
        'Scroll up
        Me.UltraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineUp)
    ElseIf pointInGridCoords.Y > grid.Height - 20 Then
        'Scroll down
        Me.UltraGrid1.ActiveRowScrollRegion.Scroll(RowScrollAction.LineDown)
    End If
End Sub

Finally, handle the UltraGrid’s DragDrop event and get the position on the grid where the rows are to be dropped. Once we have that location, we can move the row(s) to it.

In C#:

private void ultraGrid1_DragDrop(object sender, DragEventArgs e)
{
    int dropIndex;

    // Get the position on the grid where the dragged row(s) are to be dropped.
    //get the grid coordinates of the row (the drop zone)
    UIElement uieOver = ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(ultraGrid1.PointToClient(new Point(e.X, e.Y)));

    //get the row that is the drop zone/or where the dragged row is to be dropped
    UltraGridRow ugrOver = uieOver.GetContext(typeof(UltraGridRow), true) as UltraGridRow;
    if (ugrOver != null)
    {
        dropIndex = ugrOver.Index;    //index/position of drop zone in grid
    
        //get the dragged row(s)which are to be dragged to another position in the grid
        SelectedRowsCollection SelRows (SelectedRowsCollection)e.Data.GetData (typeof(SelectedRowsCollection)) as SelectedRowsCollection;
        //get the count of selected rows and drop each starting at the dropIndex
        foreach (UltraGridRow aRow in SelRows)
        {
            //move the selected row(s) to the drop zone
            ultraGrid1.Rows.Move(aRow, dropIndex);
        }
    }
}

In 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

    'Get the position on the grid where the dragged row(s) are to be dropped.
    'get the grid coordinates of the row (the drop zone)
    Dim uieOver As UIElement = UltraGrid1.DisplayLayout.UIElement.ElementFromPoint(UltraGrid1.PointToClient(New Point(e.X, e.Y)))

    'get the row that is the drop zone/or where the dragged row is to be dropped
    Dim ugrOver As UltraGridRow = TryCast(uieOver.GetContext(GetType(UltraGridRow), True), UltraGridRow)

    If ugrOver IsNot Nothing Then
        dropIndex = ugrOver.Index    'index/position of drop zone in grid
        
        'get the dragged row(s)which are to be dragged to another position in the grid
        Dim SelRows As SelectedRowsCollection = TryCast(DirectCast(e.Data.GetData(GetType(SelectedRowsCollection)), SelectedRowsCollection), SelectedRowsCollection)
        'get the count of selected rows and drop each starting at the dropIndex
        For Each aRow As UltraGridRow In SelRows
            'move the selected row(s) to the drop zone
            UltraGrid1.Rows.Move(aRow, dropIndex)
        Next
    End If
End Sub

Samples

wingrid_cs_73_clr2_drag_drop_rows.zip
 How to drag and drop rows in a WinGrid in C#


wingrid_vb_73_clr2_drag_drop_rows.zip
 How to drag and drop rows in a WinGrid in VB.Net



How would you rate the quality of this content?
Poor -----------------------------------------> Outstanding

Tell us why you rated the content this way. (optional)