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

GridView Drag and Drop functionality, in bounded mode, using BindingList<object> as data source

1 Answer 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Iliya
Top achievements
Rank 1
Iliya asked on 01 Oct 2012, 09:44 AM
Hello

In my work, i need to implement cell drag and drop functionality between few RadGridView controls. I studied examples given on this forums and discovered that all examples use DataTable as data source object, but i use BindingList<object> data source and in this case same implementation does not work. My question, is it possible to implement drag and drop between gridView controls in bounded mode when i use BindingList<object> as data source ? If yes how can i do this.

In my sample code i use three gridView controls and one button. When button is pressed, rows are loaded to all gridViews, and when i want to drag and drop cells from one gridView to another. Bellow i copied sample code i was using, and attached screenshot of the UI.

Thanks in advance.

Iliya

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
 
namespace GridViewDragDropTutorial
{
    public partial class RadForm2 : Telerik.WinControls.UI.RadForm
    {
        public RadForm2()
        {
 
            InitializeComponent();
 
            Type3ValuesDataSource = new BindingList<Value>();
            Type1ValuesDataSource = new BindingList<Value>();
            Type2ValuesDataSource = new BindingList<Value>();
 
 
            Type3ValuesGridView.AutoGenerateColumns = false;
            GridViewTextBoxColumn gridViewTextBoxColumn = new GridViewTextBoxColumn("Name""Name");
            gridViewTextBoxColumn.Width = 100;
            Type3ValuesGridView.MasterTemplate.Columns.Add(gridViewTextBoxColumn);
 
            GridViewCheckBoxColumn gridViewCheckBoxColumn = new GridViewCheckBoxColumn("CheckBoxes");
            gridViewCheckBoxColumn.Width = 50;
            Type3ValuesGridView.MasterTemplate.Columns.Add(gridViewCheckBoxColumn);
            Type3ValuesGridView.ShowColumnHeaders = false;
 
            SubscribeForGridEvents(Type3ValuesGridView);
            Type3ValuesGridView.ReadOnly = true;
            Type3ValuesGridView.MultiSelect = true;
            Type3ValuesGridView.MasterTemplate.AllowRowReorder = true;
            Type3ValuesGridView.GridBehavior = new MyBaseGridBehavior();
            Type3ValuesGridView.CellFormatting += GridViewCellFormatting;
            Type3ValuesGridView.RowFormatting += GridViewRowFormatting;
 
            Type3ValuesGridView.DataSource = Type3ValuesDataSource;
 
            Type1ValuesGridView.AutoGenerateColumns = false;
            GridViewTextBoxColumn columnLabelsGridViewColumn = new GridViewTextBoxColumn("Name""Name");
            columnLabelsGridViewColumn.Width = 210;
            Type1ValuesGridView.MasterTemplate.Columns.Add(columnLabelsGridViewColumn);
            Type1ValuesGridView.ShowColumnHeaders = false;
 
            SubscribeForGridEvents(Type1ValuesGridView);
            Type1ValuesGridView.ReadOnly = true;
            Type1ValuesGridView.MultiSelect = true;
            Type1ValuesGridView.MasterTemplate.AllowRowReorder = true;
            Type1ValuesGridView.GridBehavior = new MyBaseGridBehavior();
            Type1ValuesGridView.CellFormatting += GridViewCellFormatting;
            Type1ValuesGridView.RowFormatting += GridViewRowFormatting;
 
            Type1ValuesGridView.DataSource = Type1ValuesDataSource;
 
            Type2ValuesGridView.AutoGenerateColumns = false;
            GridViewTextBoxColumn rowLabelsGridViewColumn = new GridViewTextBoxColumn("Name""Name");
            rowLabelsGridViewColumn.Width = 210;
            Type2ValuesGridView.MasterTemplate.Columns.Add(rowLabelsGridViewColumn);
            Type2ValuesGridView.ShowColumnHeaders = false;
 
            SubscribeForGridEvents(Type2ValuesGridView);
            Type2ValuesGridView.ReadOnly = true;
            Type2ValuesGridView.MultiSelect = true;
            Type2ValuesGridView.MasterTemplate.AllowRowReorder = true;
            Type2ValuesGridView.GridBehavior = new MyBaseGridBehavior();
            Type2ValuesGridView.CellFormatting += GridViewCellFormatting;
            Type2ValuesGridView.RowFormatting += GridViewRowFormatting;
 
            Type2ValuesGridView.DataSource = Type2ValuesDataSource;
        }
 
        public BindingList<Value> Type3ValuesDataSource { getset; }
 
        public BindingList<Value> Type1ValuesDataSource { getset; }
 
        public BindingList<Value> Type2ValuesDataSource { getset; }
 
 
        private void OnStartButtonClick(object sender, EventArgs eventArgs)
        {
            InitializeDataSource(Type3ValuesDataSource);
            InitializeDataSource(Type1ValuesDataSource);
            InitializeDataSource(Type2ValuesDataSource);
        }
 
        private void InitializeDataSource(BindingList<Value> dataSource)
        {
            for (int i = 0; i < 10; i++)
            {
                Value value = new Value();
                value.Name = string.Format("Value{0}"Value.ValueIndexCounter);
                dataSource.Add(value);
 
                Value.ValueIndexCounter++;
            }
 
        }
 
 
        private void SubscribeForGridEvents(RadGridView gridView)
        {
            RadDragDropService dragDropService = gridView.GridViewElement.GetService<RadDragDropService>();
            dragDropService.PreviewDragStart += DragDropServicePreviewDragStart;
            dragDropService.PreviewDragOver += DragDropPreviewOver;
            dragDropService.PreviewDragDrop += DragDropServicePreviewDragDrop;
            //dragDropService.PreviewDragHint += new EventHandler<PreviewDragHintEventArgs>(dragDropService_PreviewDragHint);
        }
 
        private void DragDropServicePreviewDragDrop(object sender, Telerik.WinControls.RadDropEventArgs e)
        {
            // you should mark the event as handled
            // this will prevent executing of default drag-n-drop logic for unbound mode
            // which is not working in bound mode
            e.Handled = true;
            // YOUR LOGIC HERE
        }
 
        private void DragDropServicePreviewDragStart(object sender, Telerik.WinControls.PreviewDragStartEventArgs e)
        {
            e.CanStart = true;
 
        }
 
        private void GridViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            e.CellElement.AllowDrag = true;
            e.CellElement.AllowDrop = true;
        }
 
        private void GridViewRowFormatting(object sender, RowFormattingEventArgs e)
        {
            // This enables row elements to be dragged and dropped
            e.RowElement.AllowDrag = true;
            e.RowElement.AllowDrop = true;
        }
 
        private void DragDropPreviewOver(object sender, RadDragOverEventArgs e)
        {
         
        }
 
 
 
        public class MyBaseGridBehavior : BaseGridBehavior
        {
            protected override bool OnMouseDownLeft(MouseEventArgs e)
            {
                bool result = base.OnMouseDownLeft(e);
 
                if (!this.GridViewElement.IsInEditMode && this.GridViewElement.CurrentCell == this.CellAtPoint &&
                    this.CellAtPoint != null)
                {
                    RadGridViewDragDropService service = this.GridViewElement.GetService<RadGridViewDragDropService>();
                    service.Start(this.CellAtPoint);
                }
 
                return result;
            }
        }
    }
 
    public class Value
    {
        public static int ValueIndexCounter;
 
        public string Name { getset; }
    }
}

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 03 Oct 2012, 01:32 PM
Hello IIiya,

The code snippet that you are using does not work in cooperation with the row reordering feature - both scenarios are incompatible. You should use the following code snippet to achieve the desired effect:
public partial class RadForm2 : Telerik.WinControls.UI.RadForm
    {
        public RadForm2()
        {
 
            InitializeComponent();
 
            Type3ValuesDataSource = new BindingList<Value>();
            Type1ValuesDataSource = new BindingList<Value>();
            Type2ValuesDataSource = new BindingList<Value>();
 
 
            Type3ValuesGridView.AutoGenerateColumns = false;
            GridViewTextBoxColumn gridViewTextBoxColumn = new GridViewTextBoxColumn("Name", "Name");
            gridViewTextBoxColumn.Width = 100;
            Type3ValuesGridView.MasterTemplate.Columns.Add(gridViewTextBoxColumn);
 
            GridViewCheckBoxColumn gridViewCheckBoxColumn = new GridViewCheckBoxColumn("CheckBoxes");
            gridViewCheckBoxColumn.Width = 50;
            Type3ValuesGridView.MasterTemplate.Columns.Add(gridViewCheckBoxColumn);
            Type3ValuesGridView.ShowColumnHeaders = false;
 
            SubscribeForGridEvents(Type3ValuesGridView);
            Type3ValuesGridView.ReadOnly = true;
            Type3ValuesGridView.MultiSelect = true;
            Type3ValuesGridView.GridBehavior = new MyBaseGridBehavior();
            Type3ValuesGridView.CellFormatting += GridViewCellFormatting;
 
            Type3ValuesGridView.DataSource = Type3ValuesDataSource;
 
            Type1ValuesGridView.AutoGenerateColumns = false;
            GridViewTextBoxColumn columnLabelsGridViewColumn = new GridViewTextBoxColumn("Name", "Name");
            columnLabelsGridViewColumn.Width = 210;
            Type1ValuesGridView.MasterTemplate.Columns.Add(columnLabelsGridViewColumn);
            Type1ValuesGridView.ShowColumnHeaders = false;
 
            SubscribeForGridEvents(Type1ValuesGridView);
            Type1ValuesGridView.ReadOnly = true;
            Type1ValuesGridView.MultiSelect = true;
            Type1ValuesGridView.GridBehavior = new MyBaseGridBehavior();
            Type1ValuesGridView.CellFormatting += GridViewCellFormatting;
 
            Type1ValuesGridView.DataSource = Type1ValuesDataSource;
 
            Type2ValuesGridView.AutoGenerateColumns = false;
            GridViewTextBoxColumn rowLabelsGridViewColumn = new GridViewTextBoxColumn("Name", "Name");
            rowLabelsGridViewColumn.Width = 210;
            Type2ValuesGridView.MasterTemplate.Columns.Add(rowLabelsGridViewColumn);
            Type2ValuesGridView.ShowColumnHeaders = false;
 
            SubscribeForGridEvents(Type2ValuesGridView);
            Type2ValuesGridView.ReadOnly = true;
            Type2ValuesGridView.MultiSelect = true;
            Type2ValuesGridView.GridBehavior = new MyBaseGridBehavior();
            Type2ValuesGridView.CellFormatting += GridViewCellFormatting;
 
            Type2ValuesGridView.DataSource = Type2ValuesDataSource;
        }
 
        public BindingList<Value> Type3ValuesDataSource { get; set; }
 
        public BindingList<Value> Type1ValuesDataSource { get; set; }
 
        public BindingList<Value> Type2ValuesDataSource { get; set; }
 
 
        private void OnStartButtonClick(object sender, EventArgs eventArgs)
        {
            InitializeDataSource(Type3ValuesDataSource);
            InitializeDataSource(Type1ValuesDataSource);
            InitializeDataSource(Type2ValuesDataSource);
        }
 
        private void InitializeDataSource(BindingList<Value> dataSource)
        {
            for (int i = 0; i < 10; i++)
            {
                Value value = new Value();
                value.Name = string.Format("Value{0}", Value.ValueIndexCounter);
                dataSource.Add(value);
 
                Value.ValueIndexCounter++;
            }
 
        }
 
        private void SubscribeForGridEvents(RadGridView gridView)
        {
            RadDragDropService dragDropService = gridView.GridViewElement.GetService<RadDragDropService>();
            dragDropService.PreviewDragStart += DragDropServicePreviewDragStart;
            dragDropService.PreviewDragOver += DragDropPreviewOver;
            dragDropService.PreviewDragDrop += DragDropServicePreviewDragDrop;
            //dragDropService.PreviewDragHint += new EventHandler<PreviewDragHintEventArgs>(dragDropService_PreviewDragHint);
        }
 
        private void DragDropServicePreviewDragDrop(object sender, Telerik.WinControls.RadDropEventArgs e)
        {
            // you should mark the event as handled
            // this will prevent executing of default drag-n-drop logic for unbound mode
            // which is not working in bound mode
            e.Handled = true;
            // YOUR LOGIC HERE
        }
 
        private void DragDropServicePreviewDragStart(object sender, Telerik.WinControls.PreviewDragStartEventArgs e)
        {
            e.CanStart = true;
        }
 
        private void GridViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            e.CellElement.AllowDrag = true;
            e.CellElement.AllowDrop = true;
        }
 
        private void DragDropPreviewOver(object sender, RadDragOverEventArgs e)
        {
            if (e.DragInstance is GridCellElement && e.HitTarget is GridCellElement)
            {
                e.CanDrop = true;
            }
        }
 
        public class MyBaseGridBehavior : BaseGridBehavior
        {
            protected override bool OnMouseDownLeft(MouseEventArgs e)
            {
                bool result = base.OnMouseDownLeft(e);
 
                if (!this.GridViewElement.IsInEditMode && this.GridViewElement.CurrentCell == this.CellAtPoint &&
                    this.CellAtPoint != null)
                {
                    RadGridViewDragDropService service = this.GridViewElement.GetService<RadGridViewDragDropService>();
                    service.Start(this.CellAtPoint);
                }
 
                return result;
            }
        }
 
        public class Value
        {
            public static int ValueIndexCounter;
 
            public string Name { get; set; }
        }
    }

I hope this helps.
 
All the best,
Svett
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Iliya
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or