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

How to drag & drop within GridView (Unbound)

6 Answers 220 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bala
Top achievements
Rank 1
Bala asked on 24 May 2013, 04:03 PM
Hello, 
I am using RadControls for WinForms 2011 Q3 
RadGridView,  it is in unbound mode.
 
I want to reorder the row. Say I drag the row 1 and drop it to row 4. 
but it does not work. I guess somehow I must miss something, please help.  thanks !

public init()
{
            InitializeComponent();
            InitializePreviewGrid();
            SubscribeForGridEvents(radGridViewPreview);    
}
        List<PreviewItem> _previewLists = new List<PreviewItem>();
        private void InitializePreviewGrid()
        {
            radGridViewPreview.AutoGenerateColumns = false;
            var imageCol = new GridViewImageColumn("DeleteIcon", "DeleteIcon");
            imageCol.HeaderText = "";
            imageCol.Width = 30;
            radGridViewPreview.Columns.Add(imageCol);

            var col = new GridViewTextBoxColumn("DisplayStr", "DisplayStr");
            col.Width = 350;
            radGridViewPreview.Columns.Add(col);

            col = new GridViewTextBoxColumn("ValueStr", "ValueStr");
            col.IsVisible = false;
            radGridViewPreview.Columns.Add(col);

            radGridViewPreview.AllowDeleteRow = false;
            radGridViewPreview.AllowAddNewRow = false;
            radGridViewPreview.AllowEditRow = false;
            radGridViewPreview.AutoScroll = true;
            radGridViewPreview.AllowAutoSizeColumns = true;

            radGridViewPreview.MasterTemplate.AllowRowReorder = true;
            radGridViewPreview.ReadOnly = true;
            radGridViewPreview.MultiSelect = true;
            radGridViewPreview.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

            radGridViewPreview.GridBehavior = new CustomGridBehavior();
        }

        private void SubscribeForGridEvents(RadGridView grid)
        {
            dragDropService = grid.GridViewElement.GetService<RadDragDropService>();
            dragDropService.PreviewDragOver += dragDropService_PreviewDragOver;
            dragDropService.PreviewDragDrop += dragDropService_PreviewDragDrop;
            dragDropService.PreviewDragHint += dragDropService_PreviewDragHint;
            dragDropService.PreviewDropTarget += dragDropService_PreviewDropTarget;
            dragDropService.PreviewDragStart += dragDropService_PreviewDragStart;
        }

       void dragDropService_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void dragDropService_PreviewDropTarget(object sender, PreviewDropTargetEventArgs e)
        {
            //throw new NotImplementedException();
        }
        
        private void dragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)
        {
            var rowElement = e.DragInstance as GridDataRowElement;
            if (rowElement == null)
            {
                return;
            }

            var dropTarget = e.HitTarget as RadItem;
            var targetGrid = dropTarget.ElementTree.Control as RadGridView;
            if (targetGrid == null)
            {
                return;
            }

            var dragGrid = rowElement.ElementTree.Control as RadGridView;
            if (targetGrid != dragGrid)
            {
                e.Handled = true;

                var behavior = (CustomGridBehavior)dragGrid.GridBehavior;
                var dropTargetRow = dropTarget as GridDataRowElement;
                int index = dropTargetRow != null ? GetTargetRowIndex(dropTargetRow, e.DropLocation) : targetGrid.RowCount;
                MoveRows(targetGrid, dragGrid, behavior.SelectedRows, index);
            }
        }

        private void dragDropService_PreviewDragOver(object sender, RadDragOverEventArgs e)
        {
            if (e.DragInstance is GridDataRowElement)
            {
                e.CanDrop = e.HitTarget is GridDataRowElement || e.HitTarget is GridTableElement || e.HitTarget is GridSummaryRowElement;
            }
        }

        private void dragDropService_PreviewDragHint(object sender, PreviewDragHintEventArgs e)
        {
            GridDataRowElement dataRowElement = e.DragInstance as GridDataRowElement;

            if (dataRowElement != null && dataRowElement.ViewTemplate.MasterTemplate.SelectedRows.Count > 1)
            {
                //e.DragHint = new Bitmap(this.imageList1.Images[6]);
                e.UseDefaultHint = false;
            }
        }

        private void MoveRows(RadGridView targetGrid, RadGridView dragGrid, IList<GridViewRowInfo> dragRows, int index)
        {
            for (int i = dragRows.Count - 1; i >= 0; i--)
            {
                GridViewRowInfo row = dragRows[i];

                if (row is GridViewSummaryRowInfo)
                {
                    continue;
                }

                GridViewRowInfo newRow = targetGrid.Rows.NewRow();
                this.InitializeRow(newRow, row);
                targetGrid.Rows.Insert(index, newRow);
                row.IsSelected = false;
                dragGrid.Rows.Remove(row);
                index++;
            }
        }

        private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
        {
            destRow.Cells["DisplayStr"].Value = sourceRow.Cells["DisplayStr"].Value;
            destRow.Cells["ValueStr"].Value = sourceRow.Cells["ValueStr"].Value;
        }

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

6 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 27 May 2013, 09:57 AM
Hello Bala,

Is the unbound drag drop a requirement here? 
Please take a look at the code library project here for a full bound d&d solution.

Best Regards,
Emanuel Varga, MVP
0
Stefan
Telerik team
answered on 29 May 2013, 10:45 AM
Hi Bala,

Thank you for writing.

Row reordering is supported by default in unbound mode. Just set the following property to enable it:
radGridView1.AllowRowReorder

If you are looking for a drag and drop, besides Emanuel's article, you can also have a look at the following blog post: http://blogs.telerik.com/winformsteam/posts/13-05-15/extending-radgridview-to-enable-row-drag-and-drop-functionality.

I hope this helps.
 
Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
0
Bala
Top achievements
Rank 1
answered on 29 May 2013, 09:59 PM
Thank you very much! 
I got it working by tweeking example from http://blogs.telerik.com/winformsteam/posts/13-05-15/extending-radgridview-to-enable-row-drag-and-drop-functionality.

Now another issue needs your help
I have a radListView, I want to drag items from radListView to the radGridView
So the radGridView can accept dragged items from itself & radListView 
Do you have any example for this?

Thanks 
0
Stefan
Telerik team
answered on 03 Jun 2013, 01:11 PM
Hi Bala,

We do not have such an example, however you can easily achieve the desired behavior by using the same drag and drop service on RadListView. Attached you can find a sample which aims to put you on the right track.

I hope that you find this useful.

Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
0
Bhavesh
Top achievements
Rank 1
answered on 23 Oct 2013, 06:08 AM
Hi,
I have updated extended radgridview example  and add AllowRowReorder but still its not working. Can you please help me.
public Form1()
        {
            InitializeComponent();
            leftGrid.ShowGroupPanel = false;
            rightGrid.ShowGroupPanel = false;
            leftGrid.AllowAddNewRow = false;
            rightGrid.AllowAddNewRow = false;
            leftGrid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            rightGrid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            leftGrid.AllowRowReorder = true;
            rightGrid.AllowRowReorder = true;

        }

Thanks
0
Stefan
Telerik team
answered on 28 Oct 2013, 07:35 AM
Hello Bhavesh,

To add row reordering in the drag and drop solution from the blog, you have to add an else statement handling this case in the svc_PreviewDragDrop event handler. Here is how it should look like:
private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
   {
       var rowElement = e.DragInstance as GridDataRowElement;
 
       if (rowElement == null)
       {
           return;
       }
       e.Handled = true;
 
       var dropTarget = e.HitTarget as RadItem;
       var targetGrid = dropTarget.ElementTree.Control as RadGridView;
       if (targetGrid == null)
       {
           return;
       }
 
       var dragGrid = rowElement.ElementTree.Control as RadGridView;
       if (targetGrid != dragGrid)
       {
           e.Handled = true;
           //append dragged rows to the end of the target grid
           int index = targetGrid.RowCount;
 
           //Grab every selected row from the source grid, including the current row
           List<GridViewRowInfo> rows =
                               dragGrid.SelectedRows.ToList<GridViewRowInfo>();
           if (dragGrid.CurrentRow != null)
           {
               GridViewRowInfo row = dragGrid.CurrentRow;
               if (!rows.Contains(row))
                   rows.Add(row);
           }
           this.MoveRows(targetGrid, dragGrid, rows, index);
       }
       else if (dragGrid.SelectedRows.Count ==1)
       {
           int oldIndex = -1;
           int newIndex = dragGrid.Rows.Count-1;
           for (int i = 0; i < dragGrid.Rows.Count; i++)
           {
               if (dragGrid.Rows[i] == rowElement.RowInfo)
               {
                   oldIndex = i;
               }
 
               if (dropTarget is GridDataRowElement && dragGrid.Rows[i] == ((GridDataRowElement)dropTarget).RowInfo)
               {
                   newIndex = i;
               }
 
           }
           e.Handled = true;
           dragGrid.Rows.Move(oldIndex, newIndex);
       }
   }

I hope this helps.

Regards,
Stefan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Bala
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Stefan
Telerik team
Bala
Top achievements
Rank 1
Bhavesh
Top achievements
Rank 1
Share this question
or