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 BasicPrivate 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
Hi Jannet,
I am fammiliar with this option, but this also disables the groupiong option and the hide and show columns.
I want to only disable the column order option.
Hello Liran,
Yes, you are right.
With this option enabled, since a column header cannot be moved, you cannot group data against this column or hide it by dragging its header. You can perform these actions in other ways.
Please see my updated answer for further information. You are welcome to test the attached sample and ask me if you have additional questions.
Hello Jannet,
The second approach you suggested works almost perfectly, execpt for one littel issue, when I remove a column I can't add it back… I found a bypass for that: to put the column in the group panel and add it from there, but I can't tell if other users will have the same resourcefulness because this is not so intuitive…
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:
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 (-:
You are welcome!