KB Article T126930
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

How to disable dragging column headers?

GridView - How to disable column reordering but keep the column grouping and hiding functionalities

Description:
I use a GridControl component with the GridView and I need to disable the capability to reorder columns by an end-user. At the same time, I want to keep all other functionalities such as grouping, removing, adding and other. How can I achieve this?

Answer:
Hello,

Our GridView allows a user to completely disable the capability to move columns by dragging their headers out of the box. For that, you can disable the GridView.OptionsCustomization.AllowColumnMoving option. However, when the option is disabled, it is also impossible to group data against this column or hide it by dragging its header since the column header cannot be moved. To avoid this limitation, you can perform these actions in other ways.

The first way is to use the grid column's context menu:
To group data by a specific column, right-click the column header and select the 'Group By This Column' menu item.
To hide a specific column, right-click its header and select the 'Remove This Column' menu item.

The second approach that I suggest is to leave the GridView.OptionsCustomization.AllowColumnMoving option enabled and handle the GridView.DragObjectOver event to control column header dragging:

C#
private void gridView_DragObjectOver(object sender, DevExpress.XtraGrid.Views.Base.DragObjectOverEventArgs e) { var dragObject = e.DragObject; if(!(dragObject is GridColumn)) { return; } var column = dragObject as GridColumn; if(column.GroupIndex >= 0) { return; } var index = e.DropInfo.Index; GridView view = sender as GridView; bool isInGroupPanel = GetIsInGroupPanel(view); if(!isInGroupPanel && index >= 0 && column.Visible) { e.DropInfo.Valid = false; } } private bool GetIsInGroupPanel(GridView view) { var hitInfo = GetHitInfo(view); bool isInGroupPanel = hitInfo.InGroupPanel; return isInGroupPanel; } private DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo GetHitInfo(GridView view) { if (view == null) { return null; } var mousePosition = Control.MousePosition; var hitInfo = view.CalcHitInfo(view.GridControl.PointToClient(mousePosition)); return hitInfo; }
Visual Basic
Private Sub gridView1_DragObjectOver(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.DragObjectOverEventArgs) Handles gridView1.DragObjectOver Dim dragObject = e.DragObject If Not(TypeOf dragObject Is GridColumn) Then Return End If Dim column = TryCast(dragObject, GridColumn) If column.GroupIndex >= 0 Then Return End If Dim index = e.DropInfo.Index Dim view As GridView = TryCast(sender, GridView) Dim isInGroupPanel As Boolean = GetIsInGroupPanel(view) If (Not isInGroupPanel) AndAlso index >= 0 AndAlso column.Visible Then e.DropInfo.Valid = False End If End Sub Private Function GetIsInGroupPanel(ByVal view As GridView) As Boolean Dim hitInfo = GetHitInfo(view) Dim isInGroupPanel As Boolean = hitInfo.InGroupPanel Return isInGroupPanel End Function Private Function GetHitInfo(ByVal view As GridView) As DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo If view Is Nothing Then Return Nothing End If Dim mousePosition = Control.MousePosition Dim hitInfo = view.CalcHitInfo(view.GridControl.PointToClient(mousePosition)) Return hitInfo End Function

Attached is a sample illustrating how to use the second approach.

See also
Hit Information Overview
Samples of Using Hit Information
DragObjectOverEventArgs.DropInfo

Show previous comments (3)
DevExpress Support Team 11 years ago

    Hi Liran,
    OK, I see this. I've updated my answer to fulfil this requirement.
    I've added a check for the GridColumn.Visible property in the gridView1_DragObjectOver event handler:

    C#
    private void gridView1_DragObjectOver(object sender, DevExpress.XtraGrid.Views.Base.DragObjectOverEventArgs e) { var dragObject = e.DragObject; if(!(dragObject is GridColumn)) { return; } var column = dragObject as GridColumn; if(column.GroupIndex >= 0) { return; } var index = e.DropInfo.Index; GridView view = sender as GridView; bool isInGroupPanel = GetIsInGroupPanel(view); if(!isInGroupPanel && index >= 0 && column.Visible /* < -- ADDED*/) { e.DropInfo.Valid = false; } }

    Now a user can remove a column and then add it using the Column Chooser. Please check it and let me know your results.

      Works perfectly, Thanks (-:

      DevExpress Support Team 11 years ago

        You are welcome!

        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.