sfDataGrid groups are collapsing after I refresh the data by re-assigning the DataSource property

sfDataGrid groups are collapsing after I refresh the data by re-assigning the DataSource property. I tried calling on ExpandGroup() after I assign the DataSource property, I tried setting Grid.View.AutoExpandGroups = true, no


1 Reply

VS Vijayarasan Sivanandham Syncfusion Team January 31, 2022 11:39 AM UTC

Hi Truth Seeker,

If you want to maintain the groups after resetting the DataSource, you need to manually save the expanded groups and you should manually expand that group in DataSourceChanged event in SfDataGrid. Please refer the below code snippet, 
List<Group> expandedGroups = null; 
List<string> columnName = null; 
         
DataTable employeeCollection; 
public Form1() 
{ 
            InitializeComponent(); 
            expandedGroups = new List<Group>(); 
            columnName = new List<string>(); 
            this.sfDataGrid1.AutoGenerateColumns = true; 
            var table = this.GetDataTable();            
            sfDataGrid1.DataSource = table; 
            this.sfDataGrid1.GroupColumnDescriptions.Add(new GroupColumnDescription() { ColumnName = "CustomerID" }); 
            this.sfDataGrid1.ExpandAllGroup(); 
            this.sfDataGrid1.DataSourceChanged += OnDataSourceChanged; 
             
} 
 
private void OnDataSourceChanged(object sender, Syncfusion.WinForms.DataGrid.Events.DataSourceChangedEventArgs e) 
{ 
            sfDataGrid1.BeginInvoke(new Action(() => 
            { 
            if (e.NewValue !=null && columnName.Count > 0) 
            { 
                foreach (var col in columnName) 
                { 
                    this.sfDataGrid1.GroupColumnDescriptions.Add(new GroupColumnDescription() { ColumnName = col }); 
                } 
 
                if (sfDataGrid1.View.TopLevelGroup != null) 
                { 
 
                    foreach (Group group in sfDataGrid1.View.TopLevelGroup.Groups) 
                    { 
                        var isExpandGroup = group; 
                        var key = expandedGroups.FirstOrDefault(colu => colu.Key.ToString() == isExpandGroup.Key.ToString()); 
                        do 
                        { 
                            if (key != null) 
                                sfDataGrid1.ExpandGroup(isExpandGroup); 
 
                            if (isExpandGroup.Groups != null) 
                            { 
                                isExpandGroup = isExpandGroup.Groups[0]; 
                                key = expandedGroups.FirstOrDefault(col => col.Groups[0].Key.ToString() == group.Groups[0].Key.ToString()); 
                            } 
                            else 
                                isExpandGroup = null; 
                        } while (isExpandGroup != null); 
                    } 
                } 
            } 
            })); 
} 
 
private void OnDataSourceChangeClicked(object sender, EventArgs e) 
{ 
            var groups = sfDataGrid1.View.TopLevelGroup.Groups; 
            foreach (Group group in groups) 
            { 
                if (group.IsExpanded) 
                    expandedGroups.Add(group); 
            } 
 
            foreach (GroupColumnDescription groupColumnDescriptions in sfDataGrid1.GroupColumnDescriptions) 
                columnName.Add(groupColumnDescriptions.ColumnName); 
             
            this.sfDataGrid1.DataSource = GetDataTable1(); 
} 

Sample Link: https://www.syncfusion.com/downloads/support/forum/172393/ze/SfDataGridDemo-1287460302

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Loader.
Up arrow icon