Current filter:
                                You should refresh the page.
                                  • Hi Team,

                                    I would like to enable drag&drop support between grid column headers in order to implement a copy functionality for values from one column to another. I would like to use this instead of moving columns.

                                    The existing drag&drop functionality seems to be centered on dragging records and I only found proposed solutions for column headers as drop target (e.g. Q582763).

                                    How can I start drag operations from column headers?

                                1 Solution

                                Creation Date Importance Sort by

                                Hi Frank,
                                To implement your custom logic for column dragging, disable GridControl's Drag&Drop by setting the TableView.AllowColumnMoving property to false and use the system Drag&Drop mechanism. For this, create an implicit style for the GridColumnHeader element and subscribe to the PreviewMouseDown and Drop events and set AllowDrop to true:

                                [XAML]
                                <Style TargetType="dxg:GridColumnHeader"> <Setter Property="AllowDrop" Value="True"/> <EventSetter Event="PreviewMouseDown" Handler="GridColumnHeader_PreviewMouseDown"/> <EventSetter Event="Drop" Handler="GridColumnHeader_Drop"/> </Style>

                                I have attached a simple example to illustrate the main idea of this approach. Please take a moment to review it and let us know if you need further assistance.

                                Thanks,
                                Alexander

                                • Frank Mazannek 07.29.2019

                                  Thank you Alexander. This solution works for me.

                                  I only had to add some code for drag gesture recognition, because my header contains clickable content and the click was interpreted as drag operation.

                                  This is what I ended up in:

                                  [XAML]
                                  <Style TargetType="dxg:GridColumnHeader"> <Setter Property="AllowDrop" Value="True"/> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="GridColumnHeader_MouseLeftButtonDown"/> <EventSetter Event="PreviewMouseLeftButtonUp" Handler="GridColumnHeader_MouseLeftButtonUp"/> <EventSetter Event="PreviewMouseMove" Handler="GridColumnHeader_PreviewMouseMove"/> <EventSetter Event="DragOver" Handler="GridColumnHeader_DragOver"/> <EventSetter Event="Drop" Handler="GridColumnHeader_Drop"/> </Style>
                                  [C#]
                                  private void GridColumnHeader_DragOver(object sender, DragEventArgs e) { var (sourceColumn, targetColumn) = GetDragContext(sender, e); if (targetColumn.AllowEditing == DefaultBoolean.False || sourceColumn == targetColumn) { e.Effects = DragDropEffects.None; } } private void GridColumnHeader_Drop(object sender, DragEventArgs e) { var (sourceColumn, targetColumn) = GetDragContext(sender, e); var grid = sourceColumn?.View.DataControl as GridControl; for (var row = 0; row < grid?.VisibleRowCount; row++) { grid.SetCellValue(row, targetColumn, grid.GetCellValue(row, sourceColumn)); } } private void GridColumnHeader_PreviewMouseMove(object sender, MouseEventArgs e) { if (_isMouseDown && IsDragGesture(e.GetPosition(e.Source as FrameworkElement))) { // Drag Started _isMouseDown = false; var column = (GridColumnHeader)sender; DragDrop.DoDragDrop(column, column.DataContext, DragDropEffects.Copy); } } private void GridColumnHeader_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount != 1) { return; } _dragStartPoint = e.GetPosition(e.Source as FrameworkElement); _isMouseDown = true; } private void GridColumnHeader_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { _isMouseDown = false; } private static (GridColumn sourceColumn, GridColumn targetColumn) GetDragContext(object sender, DragEventArgs e) { var header = (GridColumnHeader) sender; var sourceColumn = (GridColumn) e.Data.GetData(typeof(GridColumn)); var targetColumn = (GridColumn) header.DataContext; return (sourceColumn, targetColumn); } private bool IsDragGesture(Point point) { var horizontalMove = Math.Abs(point.X - _dragStartPoint.X); var verticalMove = Math.Abs(point.Y - _dragStartPoint.Y); var hGesture = horizontalMove > SystemParameters.MinimumHorizontalDragDistance; var vGesture = verticalMove > SystemParameters.MinimumVerticalDragDistance; return hGesture || vGesture; }
                                • Alexander Rus (DevExpress Support) 07.29.2019

                                  Thank you for sharing your solution with us, Frank. I am happy to hear that you were able to achieve your goal.