Description:
I currently have a summary count of all the entries in a particular column (i.e. total number of records) and it is currently shown at the bottom of this column in the footer. However, I want this summary to always be visible in the same location regardless of whether the user has dragged the column to another location. Is there any way to do this?
Answer:
Yes, you can achieve this task by handling the GridView’s DragObjectStart and DragObjectDrop events as shown below:
C#private void gridView1_DragObjectDrop(object sender, DevExpress.XtraGrid.Views.Base.DragObjectDropEventArgs e) {
if ((e.DragObject is DevExpress.XtraGrid.Columns.GridColumn) && (e.DropInfo is DevExpress.XtraGrid.Dragging.ColumnPositionInfo)) {
if ((dragVisibleIndex == 0) || ((e.DropInfo as DevExpress.XtraGrid.Dragging.ColumnPositionInfo).Index == 0)) {
(sender as DevExpress.XtraGrid.Views.Grid.GridView).Columns[dragAbsIndex].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.None;
(sender as DevExpress.XtraGrid.Views.Grid.GridView).VisibleColumns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
}
dragVisibleIndex = -1;
dragAbsIndex = -1;
}
}
int dragVisibleIndex = -1;
int dragAbsIndex = -1;
private void gridView1_DragObjectStart(object sender, DevExpress.XtraGrid.Views.Base.DragObjectStartEventArgs e) {
if (e.DragObject is DevExpress.XtraGrid.Columns.GridColumn){
dragVisibleIndex = (e.DragObject as DevExpress.XtraGrid.Columns.GridColumn).VisibleIndex;
dragAbsIndex = (sender as DevExpress.XtraGrid.Views.Grid.GridView).VisibleColumns[0].AbsoluteIndex;
}
}
private void Form1_Load(object sender, System.EventArgs e) {
oleDbDataAdapter1.Fill(dataSet11);
}
Visual BasicPrivate Sub GridView1_DragObjectDrop(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.DragObjectDropEventArgs) Handles GridView1.DragObjectDrop
If (TypeOf e.DragObject Is DevExpress.XtraGrid.Columns.GridColumn) And (TypeOf e.DropInfo Is DevExpress.XtraGrid.Dragging.ColumnPositionInfo) Then
If (dragVisibleIndex = 0) Or CType(e.DropInfo, DevExpress.XtraGrid.Dragging.ColumnPositionInfo).Index = 0 Then
CType(sender, DevExpress.XtraGrid.Views.Grid.GridView).Columns(dragAbsIndex).SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.None
CType(sender, DevExpress.XtraGrid.Views.Grid.GridView).VisibleColumns(0).SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count
End If
dragVisibleIndex = -1
dragAbsIndex = -1
End If
End Sub
Private dragVisibleIndex As Integer = -1
Private dragAbsIndex As Integer = -1
Private Sub GridView1_DragObjectStart(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.DragObjectStartEventArgs) Handles GridView1.DragObjectStart
If TypeOf e.DragObject Is DevExpress.XtraGrid.Columns.GridColumn Then
dragVisibleIndex = CType(e.DragObject, DevExpress.XtraGrid.Columns.GridColumn).VisibleIndex
dragAbsIndex = CType(sender, DevExpress.XtraGrid.Views.Grid.GridView).VisibleColumns(0).AbsoluteIndex
End If
End Sub
See Also:
How to catch the moment when a column's position is changed
How to prevent end-users from hiding all grid columns