Datagrid - DataGridGroupRow - control over expand / collapse

Posted by: appmod on 7 August 2017, 8:11 am EST

  • Posted 7 August 2017, 8:11 am EST

    I have a datagrid with itemsource bound to an observable collection.
    If the user uses the grouping feature, and collapses 1 (or more) groups..
    But in the background (on timer) we have newer records arriving in to our collection..
    The datagrid will always refresh with all groups expanded.

    I need a way to remember which group values are collapsed, and after each refresh (to itemsource) I can re-collapse the correct entries (assuming they still exist of course).

    This person also had a similar question:
    http://our.componentone.com/groups/silverlight/studio-for-silverlight/forum/topic/datagrid-refresh-data

    Any tips/ suggestions (or can it be done)?
    Thanks in advance.
    -michael
  • Replied 7 August 2017, 8:11 am EST

    I think you can save the 'GroupRowsVisibility' values using a dictionary and apply those values later on after the refresh. So, try something like this to save the GroupRowsVisibility values:-

     Dictionary<int, Visibility> rowvisibilityvalues = new Dictionary<int, Visibility>();

    foreach (var row in grid.Rows)
    {
    if (row.Type == DataGridRowType.Group)
    {
    rowvisibilityvalues[row.Index] = (row as DataGridGroupRow).GroupRowsVisibility;
    }
    }


    You can manually apply the values like this:-

    foreach (var row in GridA.Rows)
    {
    if (row.Type == DataGridRowType.Group)
    {
    (row as DataGridGroupRow).GroupRowsVisibility = rowvisibilityvalues[row.Index] ;
    }
    }


    Let me know if you face any further issues/questions.

    Thanks
  • Replied 7 August 2017, 8:11 am EST

    Wondering if this is still the only way to keep groups from auto-expanding after a refresh.

    If so, are there recommended Grid events to perform this manual saving / retrieving of grid settings?

    Thanks.
  • Replied 7 August 2017, 8:11 am EST

    How is the refresh occurring in your case? Is it a change in the bound ItemsSource or what?
    If you are changing the itemssource manually, you can update the ItemsSource between the above methods.

    Regards
  • Replied 20 January 2020, 4:06 am EST

    c1upkar_s said:
    I think you can save the 'GroupRowsVisibility' values using a dictionary and apply those values later on after the refresh. So, try something like this to save the GroupRowsVisibility values:-

     Dictionary<int, Visibility> rowvisibilityvalues = new Dictionary<int, Visibility>();

    foreach (var row in grid.Rows)
    {
    if (row.Type == DataGridRowType.Group)
    {
    rowvisibilityvalues[row.Index] = (row as DataGridGroupRow).GroupRowsVisibility;
    }
    }


    You can manually apply the values like this:-

    foreach (var row in GridA.Rows)
    {
    if (row.Type == DataGridRowType.Group)
    {
    (row as DataGridGroupRow).GroupRowsVisibility = rowvisibilityvalues[row.Index] ;
    }
    }


    Let me know if you face any further issues/questions.

    Thanks


    C1upkar_s, this does not seem to work. If I read the property `GroupRowsVisibility` I always get `Visible`, regardless of the situation for the group, expanded or collapsed doesn't matter.

    Any idea?

    Thanks,

    A.
  • Marked as Answer

    Replied 20 January 2020, 10:30 pm EST

    Hi,

    I have prepared for you a solution that stores the expand/collapse state of grouped rows in C1DataGrid. For details, please refer the attached application.

    Hopefully this should help.

    Regards,
    Ruchir
    StoreGroupInfo_C1DataGrid.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels