This is a migrated thread and some comments may be shown as answers.

RowIsExpandedChanged and CollapseHierarchyItem

3 Answers 237 Views
GridView
This is a migrated thread and some comments may be shown as answers.
imad
Top achievements
Rank 1
imad asked on 22 Mar 2011, 02:08 AM
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.

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 23 Mar 2011, 01:42 PM
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
0
imad
Top achievements
Rank 1
answered on 23 Mar 2011, 04:22 PM
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
0
Milan
Telerik team
answered on 28 Mar 2011, 05:20 PM

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
Tags
GridView
Asked by
imad
Top achievements
Rank 1
Answers by
Maya
Telerik team
imad
Top achievements
Rank 1
Milan
Telerik team
Share this question
or