# Row Drag and Drop

The Drag and Drop functionality allows you to reorder single or multiple Grid rows by dragging. Additionally, you can move rows between different Grids.

The Telerik UI for ASP.NET Core Grid supports two interaction modes:

* [Drag and Drop](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/row-drag-drop#getting-started) - The user can click to drag a row and release the dragged row in the desired location.
* [Click-Move-Click](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/row-drag-drop#row-click-move-click) - The user can click to initiate the dragging interaction, move the pointer to the desired location and click again to release the dragged item.

## Getting Started

To enable the Drag and Drop feature:

1. Set the row reordering to "true" through the `Reorderable()` method.
2. Add a draggable column by using the `Draggable` option. As a result, the Grid will render a drag handle on each row and will make the rows draggable.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
           .Name("Grid")
           .Reorderable(reorder => reorder.Rows(true))
           .Columns(columns =>
           {
               columns.Template("").Draggable(true);
               columns.Bound(p => p.ShipName).Title("Ship Name");
           })
           .DataSource(dataSource => dataSource
               .Ajax()
               .PageSize(10)
               .Model(m => m.Id("OrderID")) //Ensure that the Model identifier ("Id") is defined.
               .Read(read => read.Action("Orders_Read", "Grid"))
           )
       )
   ```

   ```Razor TagHelper
       <kendo-grid name="grid" height="550">
           <datasource type="DataSourceTagHelperType.Ajax" page-size="10">
               <transport>
                   <read url="@Url.Action("Orders_Read","Grid")" />
               </transport>
               <schema data="Data" total="Total" errors="Errors">
                   <model id="OrderID"> 
                   </model>
               </schema>
           </datasource>
           <reorderable rows="true"/>
           <columns>
               <column template="" draggable="true" />
               <column field="ShipName" title="Ship Name"/>
           </columns>
       </kendo-grid>
   ```

## Drag and Drop Multiple Rows in the same Grid

To drag and drop multiple Grid rows, set the selection mode to `Multiple` through the `Selectable()` configurator. In this case, only the selected rows will be draggable. If the user drags selected rows, the current row selection will be cleared on row drop.
Additionally, the row drag clue will be `N items selected` where `N` is the number of the selected rows.

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
        .Name("Grid")
        .Reorderable(reorder => reorder.Rows(true))
        .Selectable(select => select.Mode(GridSelectionMode.Multiple))
        .Columns(columns =>
        {
            columns.Template("").Draggable(true);
            columns.Bound(p => p.ShipName).Title("Ship Name");
        })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(10)
            .Model(m => m.Id("OrderID")) //Ensure that the Model identifier ("Id") is defined.
            .Read(read => read.Action("Orders_Read", "Grid"))
        )
    )
```

```Razor TagHelper
    <kendo-grid name="grid" selectable="multiple, row">
        <datasource type="DataSourceTagHelperType.Ajax" page-size="10">
            <transport>
                <read url="@Url.Action("Orders_Read","Grid")" />
            </transport>
            <schema data="Data" total="Total" errors="Errors">
                <model id="OrderID"> 
                </model>
            </schema>
        </datasource>
        <reorderable rows="true"/>
        <columns>
            <column template="" draggable="true" />
            <column field="ShipName" title="Ship Name"/>
        </columns>
    </kendo-grid>
```

## Drag and Drop Rows between Grids

You can move items from one instance of the Grid to another by handling the [`RowReorder` event](https://docs.telerik.com/kendo-ui/api/ui/grid/events/rowreorder) of the Grid. It fires on reordering for both Grid instances and allows you to access and modify their data sources. Both Grids must be bound to the same model.

> Dragging and dropping a row into an empty Grid requires a custom implementation that adds a placeholder row to the target Grid.

For a runnable example, refer to the [demo on drag and drop by the Grid](https://demos.telerik.com/aspnet-core/grid/drag-drop).

## Row Click-Move-Click

As of Telerik UI for ASP.NET Core R2 SP1 2023, users can reorder the Grid's rows by using the click-move-click functionality provided by the `ClickMoveClick` option. To start moving the row, users can click the drag icon, and then click again to place the row in its new position.

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Template("").Draggable(true).Width(100);
            columns.Bound(p => p.ProductName).Title("Product").Width(200);
            columns.Bound(p => p.UnitPrice).Title("Price").Width(140);
            columns.Bound(p => p.Discontinued)
            .Title("In Stock")
            .ClientTemplate("<span class='k-badge k-badge-success k-badge-inline'>Available</span>")
            .Width(130);
        })
        .Pageable()
        .Scrollable()
        .Sortable()
        .Navigatable()
        .Reorderable(reorder => reorder
            .Rows(rows => rows.ClickMoveClick(true))
            .Columns(true)
        )
        .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Model(m => m.Id("ProductID"))
            .Read(read => read.Action("Drag_Drop_Active_Read", "Grid"))
        )
    )
```

```Razor TagHelper
    <kendo-grid name="grid" navigatable="true" height="430">
        <columns>
            <column template="" draggable="true" width="100">
            </column>
            <column field="ProductName" title="Product" width="200">
            </column>
            <column field="UnitPrice" title="Price" width="140">
            </column>
            <column field="Discontinued" title="In Stock" template="<span class='k-badge k-badge-success k-badge-inline'>Available</span>" width="130">
            </column>
        </columns>
        <pageable enabled="true" />
        <scrollable enabled="true" />
        <sortable enabled="true" />
        <reorderable columns="true" >
            <rows click-move-click="true" />
        </reorderable>
        <datasource type="DataSourceTagHelperType.Ajax" page-size="20">
            <schema data="Data" total="Total" errors="Errors">
                <model id="ProductID">
                </model>
            </schema>
            <transport>
                <read url="@Url.Action("Drag_Drop_Active_Read","Grid")" />
            </transport>
        </datasource>
    </kendo-grid>
```

## Drag Handle

You can allow the user to reorder the Grid rows by dragging the row through a drag handle. To enable the drag handle, add a draggable column:

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Template("").Draggable(true).Width(100);
            columns.Bound(p => p.ProductName).Title("Product").Width(200);
        })
    )
```

```Razor TagHelper
    <kendo-grid name="grid">
        <columns>
            <column template="" draggable="true" width="100"></column>
            <column field="ProductName" title="Product" width="200"></column>
        </columns>
    </kendo-grid>
```

## RowReorder Event

The Telerik UI for ASP.NET Core Grid exposes a [RowReorder](https://www.telerik.com/aspnet-core-ui/documentation/api/kendo.mvc.ui.fluent/grideventbuilder#rowreordersystemstring) event. The event fires when the user drops a row into a new location. It allows you to manipulate your data collection based on where the user dropped the element.

## Keyboard Navigation

You can reorder the rows through the keyboard when the `Navigatable()` method is enabled.

On mobile devices, the Drag and Drop functionality activates on `hold` & `slide` of the row.

The example below demonstrates how to drag and drop the Grid rows by using the keys `Ctrl + Up/Down`.

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
        .Name("Grid")
        .Reorderable(reorder => reorder.Rows(true))
        .Navigatable()
        .Columns(columns =>
        {
            columns.Template("").Draggable(true);
            columns.Bound(p => p.ShipName).Title("Ship Name");
        })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(10)
            .Model(m => m.Id("OrderID")) //Ensure that the Model identifier ("Id") is defined.
            .Read(read => read.Action("Orders_Read", "Grid"))
        )
        ...
    )
```

```Razor TagHelper
    <kendo-grid name="grid" height="550" navigatable="true">
        <datasource type="DataSourceTagHelperType.Ajax" page-size="10">
            <transport>
                <read url="@Url.Action("Orders_Read","Grid")" />
            </transport>
            <schema data="Data" total="Total" errors="Errors">
                <model id="OrderID"> 
                </model>
            </schema>
        </datasource>
        <reorderable rows="true"/>
        <columns>
            <column template="" draggable="true" />
            <column field="ShipName" title="Ship Name"/>
        </columns>
    </kendo-grid>
```

When the [`Multiple` selection](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/selection#select-modes) option is enabled for the Grid rows, the user can drag and drop the selected rows through the `Ctrl` + `Mouse left-click` combination.

## Known Limitations

* Row Drag and Drop is not supported when [sorting](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/sorting) is applied. The sort re-orders the data after every change, so a dropped row immediately returns to its sorted position instead of staying where it was placed.
* Row Drag and Drop is not supported when [filtering](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/filtering) is applied. Because the filter hides rows from the view, the drop position does not match the actual position in the full dataset, causing rows to land in the wrong place.
* Row Drag and Drop is not supported when [grouping](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/grouping/overview) is applied. Rows can only be dropped within the flat list of visible data rows, so reordering across groups is not possible.

## See Also

* [ASP.NET Core DataGrid Homepage](https://www.telerik.com/aspnet-core-ui/grid)

* [Row Drag and Drop by the Grid HtmlHelper for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/grid/drag-drop)

* [Grid Events](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/grid/events)

* [Server-Side API](https://www.telerik.com/aspnet-core-ui/documentation/api/grid)

* [Grid Client-Side API](https://docs.telerik.com/kendo-ui/api/ui/grid)
