Dragging Rows from the FlexGrid

You can use the HTML5 drag/drop API to implement row dragging from, into, or between the FlexGrid controls.

This simple example shows how you can drag rows from the grid into arbitrary elements. You can easily extend this to support move operations (by removing the row from the source grid when the operation is completed) or dropping into grids (by detecting the drop position and inserting new rows into the target grid).

Drag rows from the grid by the row header:

Id
Country
Product
Active
Downloads
Sales
Expenses
Overdue
0
US
Phones
145,248
81,732.54
38,401.13
1
Germany
Computers
111,632
20,603.32
27,944.24
2
UK
Cameras
181,205
44,217.79
48,877.49
3
Japan
Stereos
54,740
29,190.63
23,365.74
4
Italy
Phones
126,531
46,951.19
49,107.56
5
Greece
Computers
6,073
86,237.02
49,767.35

And drop them here:

Drop rows here...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// This file locates: "Scripts/Lesson/C1FlexGrid/EventsDragDrop.js".
c1.documentReady(function () {
    var theGrid = wijmo.Control.getControl('#theGrid');
 
    // allow dragging from the grid
    makeDragSource(theGrid);
 
    // allow dropping into target
    var theTarget = document.getElementById('theTarget');
    makeDropTarget(theTarget);
 
    // make grid rows draggable
    function makeDragSource(s) {
 
        // make rows draggable
        s.itemFormatter = function (panel, r, c, cell) {
            if (panel.cellType == wijmo.grid.CellType.RowHeader) {
                cell.textContent = (r + 1).toString();
                cell.draggable = true;
            }
        };
 
        // disable built-in row drag/drop
        s.hostElement.addEventListener('mousedown', function (e) {
            if (s.hitTest(e).cellType == wijmo.grid.CellType.RowHeader) {
                e.stopPropagation();
            };
        }, true);
 
        // handle drag start
        s.hostElement.addEventListener('dragstart', function (e) {
            var ht = s.hitTest(e);
            if (ht.cellType == wijmo.grid.CellType.RowHeader) {
                s.select(new wijmo.grid.CellRange(ht.row, 0, ht.row, s.columns.length - 1));
                e.dataTransfer.effectAllowed = 'copy';
                e.dataTransfer.setData('text', ht.row.toString());
            };
        }, true);
    }
 
    // enable drop operations on an element
    function makeDropTarget(s) {
        s.addEventListener('dragover', function (e) {
            var dragRow = e.dataTransfer.getData('text');
            if (dragRow != null) {
                e.dataTransfer.dropEffect = 'copy';
                e.preventDefault();
            }
        });
        s.addEventListener('drop', function (e) {
            var dragRow = e.dataTransfer.getData('text');
            if (dragRow != null) {
                var item = theGrid.rows[parseInt(dragRow)].dataItem;
                alert('thanks for dropping row ' +
                    JSON.stringify(item) +
                  ' here.');
            }
        });
    }
 
});
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1FlexGridController : Controller
    {
        // GET: EventsDragDrop
        public ActionResult EventsDragDrop()
        {
            return View(Models.FlexGridData.GetSales());
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@model IEnumerable<FlexGridData.Sale>
 
<h1>
    @Html.Raw(Resources.C1FlexGrid.EventsDragDrop_Title)
</h1>
<p>
    @Html.Raw(Resources.C1FlexGrid.EventsDragDrop_Text1)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.EventsDragDrop_Text2)
</p>
 
<p>
    @Html.Raw(Resources.C1FlexGrid.EventsDragDrop_Text3)
</p>
@Html.C1().FlexGrid().Id("theGrid").Bind(Model).Height(250)
 
<p>
    @Html.Raw(Resources.C1FlexGrid.EventsDragDrop_Text4)
</p>
<div id="theTarget" style="height:100px;background-color:#e0e0e0">
    @Html.Raw(Resources.C1FlexGrid.EventsDragDrop_Text5)
</div>