You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DevExpress WinForms XtraGrid取消分组时如何恢复列原始位置?

Great question! I’ve dealt with this exact XtraGrid behavior a few times, and there are clean solutions for both of your requirements. Let’s break them down step by step:

1. Keep Original Column Order When Removing All Grouping

First, you need to record the initial visible order of your columns because XtraGrid defaults to dropping ungrouped columns at the end of the header. Store this order when your form loads or the grid initializes:

private Dictionary<GridColumn, int> _originalColumnPositions = new Dictionary<GridColumn, int>();

private void YourForm_Load(object sender, EventArgs e)
{
    // Capture the initial visible index of each column
    foreach (GridColumn column in gridView1.Columns)
    {
        if (column.Visible)
        {
            _originalColumnPositions[column] = column.VisibleIndex;
        }
    }
}

Next, handle the GridView.EndGrouping event—this fires after any grouping operation completes, including when all groups are removed. In this event, reset the columns to their original order if no groups remain:

private void gridView1_EndGrouping(object sender, EventArgs e)
{
    GridView view = sender as GridView;
    
    // Only reset order if all grouping is removed
    if (view.GroupedColumns.Count == 0)
    {
        view.BeginUpdate(); // Prevent UI flicker and recursive events
        
        // Sort columns by their original position and reset visible indexes
        foreach (var columnEntry in _originalColumnPositions.OrderBy(entry => entry.Value))
        {
            if (columnEntry.Key.Visible)
            {
                columnEntry.Key.VisibleIndex = columnEntry.Value;
            }
        }
        
        view.EndUpdate();
    }
}

This ensures when the user clears all grouping, columns snap back to their original layout.

2. Restore Original Position When Dragging a Grouped Column Back to the Header

For this, we’ll intercept when a column is moved from the group panel back to the header, then reset its position to where it was before grouping. Use the GridView.ColumnPositionChanged event—it triggers whenever a column’s position (either in the header or group panel) changes:

private void gridView1_ColumnPositionChanged(object sender, EventArgs e)
{
    GridView view = sender as GridView;
    GridColumn movedColumn = view.FocusedColumn;
    
    // Check if the column was just ungrouped (GroupIndex went from >=0 to -1)
    if (movedColumn.GroupIndex == -1 && _originalColumnPositions.ContainsKey(movedColumn))
    {
        view.BeginUpdate();
        
        // Set the column back to its original visible index
        movedColumn.VisibleIndex = _originalColumnPositions[movedColumn];
        
        view.EndUpdate();
    }
    // Optional: Update the "original" position if the user manually reorders columns
    else if (movedColumn.GroupIndex == -1)
    {
        _originalColumnPositions[movedColumn] = movedColumn.VisibleIndex;
    }
}

The optional clause lets you update the stored "original" position if the user rearranges columns manually—so if they reorder columns first, then group/ungroup, the grid will respect their new preferred layout.

Quick Notes

  • If you dynamically add columns at runtime, make sure to update _originalColumnPositions when new columns are added to capture their initial positions.
  • Wrapping changes in BeginUpdate()/EndUpdate() prevents UI flicker and avoids triggering additional events recursively.

内容的提问来源于stack exchange,提问作者nakiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:12:02