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
|
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();
} |