Example T1109419
Visible to All Users

Blazor Grid - Incorporate Drag and Drop Support

The DevExpress Blazor Grid supports drag-and-drop operations.

Drag between two grids

Use the following API members to activate this feature for your Grids:

When a user drops rows, the ItemsDropped event fires. In its handler, update the data source: insert rows at the drop position and remove them from the initial position, if required.

Reorder Rows in a Grid

If you want to let users reorder rows, set the AllowDragRows property to true. You can test this usage scenario on the Reorder page.

Drag Rows Between Components

To let users drag rows between components, do the following:

The DropTargetMode property specifies drop position indication style:

  • A specific location defined by the TargetItem and DropPosition properties.
  • The entire data area. You need to implement custom insertion logic (for example, if your data is sorted or grouped) in the ItemsDropped event handler.

You can test this usage scenario on the Between page.

Files to Look At

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

GridDragAndDrop/Pages/Between.razor
Razor
@page "/between" <h1>Drag & Drop Between Components</h1> <br /> <div style="display: flex"> <div style="width: 500px"> <h2>First Grid</h2> <DxGrid Data="FirstGridItems" AllowDragRows="true" AllowedDropTarget="GridAllowedDropTarget.External" ItemsDropped="FirstGrid_ItemsDropped" AllowSort="false"> <Columns> <DxGridDataColumn FieldName="Name" /> </Columns> </DxGrid> </div> <div style="width: 500px; padding-left: 10px"> <h2>Second Grid</h2> <DxGrid Data="SecondGridItems" AllowDragRows="true" AllowedDropTarget="GridAllowedDropTarget.External" DropTargetMode="GridDropTargetMode.Component" ItemsDropped="SecondGrid_ItemsDropped" AllowSort="false"> <Columns> <DxGridDataColumn FieldName="Name" /> </Columns> </DxGrid> </div> </div> @code { ObservableCollection<GridDataItem> FirstGridItems { get; set; } ObservableCollection<GridDataItem> SecondGridItems { get; set; } public record GridDataItem(string Name); protected override void OnInitialized() { FirstGridItems = new ObservableCollection<GridDataItem>( new List<GridDataItem>() { new GridDataItem("Beverages"), new GridDataItem("Seafood"), new GridDataItem("Grains"), new GridDataItem("Confections"), new GridDataItem("Dairy Products") } ); SecondGridItems = new ObservableCollection<GridDataItem>( new List<GridDataItem>() { new GridDataItem("Cereals"), new GridDataItem("Meat"), new GridDataItem("Poultry"), new GridDataItem("Produce"), new GridDataItem("Frozen Entrees") } ); } async Task FirstGrid_ItemsDropped(GridItemsDroppedEventArgs args) { var droppedItem = (GridDataItem)args.DroppedItems[0]; SecondGridItems.Remove(droppedItem); var index = await args.GetTargetDataSourceIndexAsync(); FirstGridItems.Insert(index, droppedItem); } void SecondGrid_ItemsDropped(GridItemsDroppedEventArgs args) { var droppedItem = (GridDataItem)args.DroppedItems[0]; FirstGridItems.Remove(droppedItem); SecondGridItems.Insert(SecondGridItems.Count, droppedItem); } }
GridDragAndDrop/Pages/Reorder.razor
Razor
@page "/reorder" <h1>Reorder Rows in a Grid</h1> <br /> <div style="width: 500px"> <DxGrid Data="Items" AllowDragRows="true" ItemsDropped="Grid_ItemsDropped"> <Columns> <DxGridDataColumn FieldName="Name" /> </Columns> </DxGrid> </div> @code { ObservableCollection<GridDataItem> Items { get; set; } public record GridDataItem(string Name); protected override void OnInitialized() { Items = new ObservableCollection<GridDataItem>( new List<GridDataItem>() { new GridDataItem("Beverages"), new GridDataItem("Seafood"), new GridDataItem("Grains"), new GridDataItem("Confections"), new GridDataItem("Dairy Products"), new GridDataItem("Cereals"), new GridDataItem("Meat"), new GridDataItem("Poultry"), new GridDataItem("Produce"), new GridDataItem("Frozen Entrees") } ); } async Task Grid_ItemsDropped(GridItemsDroppedEventArgs args) { var droppedItem = (GridDataItem)args.DroppedItems[0]; Items.Remove(droppedItem); var index = await args.GetTargetDataSourceIndexAsync(); Items.Insert(index, droppedItem); } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.