Possible to drag and drop columns from one grid to another?

3 posts, 0 answers
  1. Howard
    Howard avatar
    2 posts
    Member since:
    Sep 2015

    Posted 12 Sep 2015 Link to this post

    Hi all,

    I am trying to drag and drop columns from grid1 to grid2 without any luck.

    Found a example here on how to drag and drop rows :

    http://www.telerik.com/help/winforms/gridview-rows-drag-and-drop.html

     

    I searched for the same but then with columns instead of rows but i could not find any examples,so i am not sure if it even is possible to do it?

    If it is possible does anyone know about a example or a tutorial on how to do this?

     

    best regards,

    H

     

     
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 14 Sep 2015 Link to this post

    Hello Howard,

    Thank you for writing.
     
    RadGridView handles the whole drag and drop operation by RadGridViewDragDropService. The PreviewDragOver event allows you to control on what targets the item being dragged can be dropped on. The PreviewDragDrop event allows you to get a handle on all the aspects of the drag and drop operation, the source (drag) grid, the destination (target) grid, as well as the item being dragged. This is where we will initiate the actual physical move of the dragged item(s) from one grid to the other. In order to implement drag and drop functionality of a column from one grid to another, you can use the sample approach below:
    public Form1()
    {
        InitializeComponent();
     
        RadDragDropService svc =
            this.radGridView1.GridViewElement.GetService<RadDragDropService>();
        svc.PreviewDragStart += svc_PreviewDragStart;
        svc.PreviewDragDrop += svc_PreviewDragDrop;
        svc.PreviewDragOver += svc_PreviewDragOver;
    }
     
    private void svc_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
    {
        e.CanStart = true;
    }
     
    private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
    {
        SnapshotDragItem draggedItem = e.DragInstance as SnapshotDragItem;
        GridHeaderCellElement targetHeaderCell = e.HitTarget as GridHeaderCellElement;
        if (draggedItem != null && targetHeaderCell != null)
        {
            GridHeaderCellElement sourceHeaderCell = draggedItem.Item as GridHeaderCellElement;
            if (sourceHeaderCell != null && targetHeaderCell.GridViewElement != sourceHeaderCell.GridViewElement)
            {
                e.CanDrop = true;
            }
            else
            {
                e.CanDrop = false;
            }
        }
    }
     
    private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
    {
        SnapshotDragItem draggedItem = e.DragInstance as SnapshotDragItem;
        GridHeaderCellElement targetHeaderCell = e.HitTarget as GridHeaderCellElement;
        if (draggedItem == null || targetHeaderCell == null)
        {
            return;
        }
        GridHeaderCellElement sourceHeaderCell = draggedItem.Item as GridHeaderCellElement;
        if (sourceHeaderCell != null)
        {
            e.Handled = true;
            sourceHeaderCell.GridViewElement.Template.Columns.Remove(sourceHeaderCell.ColumnInfo.Name);
            GridViewTemplate targetTemplate = targetHeaderCell.GridViewElement.Template;
            targetTemplate.Columns.Insert(targetTemplate.Columns.IndexOf(targetHeaderCell.ColumnInfo.Name),
                sourceHeaderCell.ColumnInfo as GridViewDataColumn );
        }
    }

    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 requirement best.

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Howard
    Howard avatar
    2 posts
    Member since:
    Sep 2015

    Posted 17 Sep 2015 in reply to Dess Link to this post

    Thanks a lot Dess!
Back to Top