RowIsExpandedChanged and CollapseHierarchyItem

4 posts, 0 answers
  1. imad
    imad avatar
    19 posts
    Member since:
    Mar 2007

    Posted 21 Mar 2011 Link to this post

    Hi,

    I am trying to have a single expand Hierarchy in my GridView, in the RowIsExpandedChanged events i am checking if the row is expanded and i am collapsing the other rows with the CollapseHierarchyItem.

    What is happening is that the RowIsExpandedChanged event is raised again when calling CollapseHierarchyItem, how to collapse rows without firing the RowIsExpandedChanged events.

    Thanks.
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 23 Mar 2011 Link to this post

    Hi imad,

    You may handle the RowIsExpandedChanged as follows:

    GridViewRow lastExpandedRow;

    private void clubsGrid_RowIsExpandedChanged(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
            {
                if((e.Row as GridViewRow).IsExpanded)
                {
                    if(lastExpandedRow != null)
                    {
                        lastExpandedRow.IsExpanded = false;
                    }
                    lastExpandedRow = e.Row as GridViewRow;
                }
            }

    This approach is similar to the one described in this blog post referring an equal scenario but with RowDetails.
     

    Greetings,
    Maya
    the Telerik team
  3. UI for WPF is Visual Studio 2017 Ready
  4. imad
    imad avatar
    19 posts
    Member since:
    Mar 2007

    Posted 23 Mar 2011 Link to this post

    Thanks Maya for the answer,

    But I Have 2 issues, First the lastExpandedRow.IsExpanded = false will refire the Event, this works fine in the example you gave but In my case i have
    private void clubsGrid_RowIsExpandedChanged(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
            {
                if((e.Row as GridViewRow).IsExpanded)
                {
                    if(lastExpandedRow != null)
                    {
                        lastExpandedRow.IsExpanded = false;
                    }
                    lastExpandedRow = e.Row as GridViewRow;
                }
                else{ // do some action}
            }

    so the else action will be fired for the previously expanded row.

    Second I dynamically instantiate grids from database (depending on some categories defined),
    and i am attaching these instances events, so i need to keep a collection of lastRow per grid. 

    Any solution for my case?

    Thanks
  5. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 28 Mar 2011 Link to this post

    Hi imad,

    You could try this modified version of the code:

    private bool changing = false;
      
    void clubsGrid_RowIsExpandedChanged(object sender, RowEventArgs e)
    {
        if(changing)
            return;
      
        this.changing = true;
      
        if ((e.Row as GridViewRow).IsExpanded)
        {
            this.clubsGrid.CollapseAllHierarchyItems();
            this.clubsGrid.ExpandHierarchyItem(e.Row.DataContext);
      
            //lastExpandedRow = e.Row as GridViewRow;
        }
      
        this.changing = false;
    }

    Notice the introduction of a special flag variable that guards the expand/collpase code of being executed multiple times.



    All the best,
    Milan
    the Telerik team
Back to Top