This is a migrated thread and some comments may be shown as answers.

Move rows with the mouse

4 Answers 157 Views
GridView
This is a migrated thread and some comments may be shown as answers.
FMorales
Top achievements
Rank 1
FMorales asked on 24 Oct 2014, 06:32 AM
Hi

anybody knows if the grid support to move rows natively with the mouse?

I need to click with the mouse on a groups of rows and move them to another position in the same grid (drag and drop)

Could anybody share an example?

Thanks a lot.

Francisco

4 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2014, 01:09 PM
Hello Francisco,

Thank you for writing.

RadGridViewDragDropService allows you to implement the desired drag and drop functionality within the same grid. Our GridView >> Drag and Drop help article is quite useful on this topic. Following the introduced approach I have achieved the desired functionality. Here is the same code snippet:
public partial class Form1 : Form
{
    BindingList<Color> colors = new BindingList<Color>()
    {
        Color.Red, Color.Blue, Color.Green, Color.Yellow,
        Color.Fuchsia, Color.Orange, Color.Purple
    };
 
    public Form1()
    {
        InitializeComponent();
 
        this.radGridView1.DataSource = colors;
        this.radGridView1.AllowRowReorder = true;
        this.radGridView1.AllowColumnReorder = true;
        this.radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;
        this.radGridView1.MultiSelect = true;
        this.radGridView1.CurrentRow = null;
         
        RadDragDropService svc = this.radGridView1.GridViewElement.GetService<RadDragDropService>();
        svc.PreviewDragStart += svc_PreviewDragStart;
        svc.PreviewDragDrop += svc_PreviewDragDrop;
        svc.PreviewDragOver += svc_PreviewDragOver;
 
        //register the custom row selection behavior
        var gridBehavior = this.radGridView1.GridBehavior as BaseGridBehavior;
        gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
        gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo),
            new RowSelectionGridBehavior());
    }
     
    private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
    {
        if (e.DragInstance is GridDataRowElement)
        {
            e.CanDrop = e.HitTarget is GridDataRowElement ||
                        e.HitTarget is GridTableElement ||
                        e.HitTarget is GridSummaryRowElement;
        }
    }
 
    //initiate the move of selected row
    private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
    {
        GridDataRowElement rowElement = e.DragInstance as GridDataRowElement;
 
        if (rowElement == null)
        {
            return;
        }
        e.Handled = true;
 
        RadItem dropTarget = e.HitTarget as RadItem;
        RadGridView targetGrid = dropTarget.ElementTree.Control as RadGridView;
        if (targetGrid == null)
        {
            return;
        }
 
        var dragGrid = rowElement.ElementTree.Control as RadGridView;
        if (targetGrid == dragGrid)
        {
            e.Handled = true;
 
            GridDataRowElement dropTargetRow = dropTarget as GridDataRowElement;
            int index = dropTargetRow != null ? this.GetTargetRowIndex(dropTargetRow, e.DropLocation) : targetGrid.RowCount;
          
            this.MoveRows(dragGrid, dragGrid.SelectedRows, index);
        }
    }
 
    private int GetTargetRowIndex(GridDataRowElement row, Point dropLocation)
    {
        int halfHeight = row.Size.Height / 2;
        int index = row.RowInfo.Index;
        if (dropLocation.Y > halfHeight)
        {
            index++;
        }
        return index;
    }
 
    //required to initiate drag and drop when grid is in bound mode
    private void svc_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
    {
        e.CanStart = true;
    }
 
    private void MoveRows(RadGridView dragGrid,
        GridViewSelectedRowsCollection selectedRows, int index)
    {
        dragGrid.BeginUpdate();
    
        if (dragGrid.DataSource != null && typeof(IList).IsAssignableFrom(dragGrid.DataSource.GetType()))
        {
            //bound to a list of objects scenario
            var sourceCollection = (IList)dragGrid.DataSource;
            for (int i = selectedRows.Count - 1; i >= 0; i--)
            {
                GridViewRowInfo row = selectedRows[i];
            
                if (row is GridViewSummaryRowInfo)
                {
                    return;
                }
                if (row.Index < index)
                {
                    index--;
                }
                sourceCollection.Remove(row.DataBoundItem);                   
                sourceCollection.Insert(index, row.DataBoundItem);
            }
        }
        else
        {
            throw new ApplicationException("Unhandled Scenario");
        }
                     
        dragGrid.EndUpdate(true);
    }
}
 
//initiates drag and drop service for clicked rows
public class RowSelectionGridBehavior : GridDataRowBehavior
{
    protected override bool OnMouseDownLeft(MouseEventArgs e)
    {
        GridDataRowElement row = this.GetRowAtPoint(e.Location) as GridDataRowElement;
        if (row != null)
        {
            RadGridViewDragDropService svc =
                this.GridViewElement.GetService<RadGridViewDragDropService>();
            svc.Start(row);
        }
        return base.OnMouseDownLeft(e);
    }
}

Note that this is just an example and it may not cover all possible cases. Feel free to modify it on a way which suits your requirements best.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
FMorales
Top achievements
Rank 1
answered on 29 Oct 2014, 02:41 PM
Hi Desislava,

I have implemented the solution in my grid and all works fine but I can only move one row each time, I can select various rows with the mouse but whrn I click to move them, it select just one row and allow me to move just the one selected row.

What I am doing wrong?

Thanks a million

Francisco Morales

0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Nov 2014, 10:50 AM
Hello Francisco,

Thank you for writing back.

I am glad that the suggested solution suits your requirement. As to the multiple selection, it is necessary to hold the Ctrl key while dragging the selected rows. Thus, you will be able to keep the selection and perform drag and drop operation for more than one row.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
FMorales
Top achievements
Rank 1
answered on 03 Nov 2014, 10:57 AM
Hi Desislava,

thank you very much for your support, yes, I discovered it las week :)

All fworks fine.

Thanks

Francisco
Tags
GridView
Asked by
FMorales
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
FMorales
Top achievements
Rank 1
Share this question
or