SFDatagrid - Expand group by Key

I can store a list of expanded groups within my sfDatagrid by using their Key. How would I set these groups to expanded using the Key the next time I load the sfDatagrid?

4 Replies 1 reply marked as answer

SS Susmitha Sundar Syncfusion Team July 15, 2020 03:40 PM UTC

Hi Martin, 
 
Thank you for using Syncfusion controls. 
 
We need some information about your requirement. Can you please provide below details? 
 
1.       How do you load the SfDataGrid second time? 
2.       Did you change the DataSource or are you using serialization approach? 
 
It will be helpful for us to check on it and provide you the solution at the earliest.   
 
Regards, 
Susmitha S 



MA Martin replied to Susmitha Sundar July 15, 2020 10:21 PM UTC

Hi Martin, 
 
Thank you for using Syncfusion controls. 
 
We need some information about your requirement. Can you please provide below details? 
 
1.       How do you load the SfDataGrid second time? 
2.       Did you change the DataSource or are you using serialization approach? 
 
It will be helpful for us to check on it and provide you the solution at the earliest.   
 
Regards, 
Susmitha S 


Hi Susmitha,

The sfdatagrid is bound to a datatable. When i request the latest data I clear the datatable and repopulate. This may change the display order of that data shown. I would just like to store in a list the groups in the sfdatagrid that the user has expanded. Then after each refresh or logon I can keep the same display 


VS Vijayarasan Sivanandham Syncfusion Team July 17, 2020 03:48 AM UTC

Hi Martin,

Thanks for the update.

We are currently working on “SFDatagrid - Expand group by Key” and we need one more business day to validate this. We will update you with further details on July 17, 2020.

We appreciate your patience until then. 
 
Regards, 
Vijayarasan S 



VS Vijayarasan Sivanandham Syncfusion Team July 17, 2020 04:53 PM UTC

Hi Martin,

Thank you for your patience.

By default, when you change the DataSource, SfDataGrid fully refreshed. So, the groups are cleared. If you want to maintain that groups, you need to manually save that expanded groups and after the DataSource changing, you should manually expand that group in DataSourceChanged event
 
 
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 += SfDataGrid1_DataSourceChanged; 
} 
 
private void SfDataGrid1_DataSourceChanged(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 button1_Click(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(); 
} 
 
 
 
Please check the sample and let us know if you need further assistance on this.  
  
Regards,  
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon