Back to Devexpress

How to: Control Drag-and-drop of Column Headers

windowsforms-3065-controls-and-libraries-data-grid-examples-miscellaneous-how-to-control-drag-and-drop-of-column-headers.md

latest1.7 KB
Original Source

How to: Control Drag-and-drop of Column Headers

  • Nov 13, 2018

The example shows how to prevent moving the “ContactName” column to the Customization Form.

To control drag-and-drop of grid columns, we handle the GridView.DragObjectOver event. The DragObjectOverEventArgs.DropInfo event’s parameter contains information on the current position where drag-and-drop will occur if the end-user drops the drag object. The Index property of the DragObjectOverEventArgs.DropInfo would be -1 when dragging over the Customization Form.

csharp
using DevExpress.XtraGrid.Views.Base;

private void bandedGridView1_DragObjectOver(object sender, DragObjectOverEventArgs e) {
    GridColumn column = e.DragObject as GridColumn;
    if (column != null) {
        e.DropInfo.Valid = !(e.DropInfo.Index == -1 && column.FieldName == "ContactName");
    }
}
vb
Imports DevExpress.XtraGrid.Views.Base

Private Sub BandedGridView1_DragObjectOver(ByVal sender As Object, _
  ByVal e As DragObjectOverEventArgs) Handles BandedGridView1.DragObjectOver
    If TypeOf e.DragObject Is GridColumn Then
        Dim column As GridColumn = CType(e.DragObject, GridColumn)        
        e.DropInfo.Valid = Not (e.DropInfo.Index = -1 And column.FieldName = "ContactName")
    End If
End Sub