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

selecting a single row in hierachical grid

5 Answers 107 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Cameron Molyneux
Top achievements
Rank 1
Cameron Molyneux asked on 23 Feb 2010, 03:55 PM
is there a way to force the grid to only select a single row even in child tables

what seems to be happening (2010 Q1) is that i can select a row in the parent table and also select a row in the child even though i have selectionmode set to single

5 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 23 Feb 2010, 05:48 PM
Hi CAMERON MOLYNEUX,

At the moment every grid (no matter if parent or child) has its own selection which is independent from any other grid. We will take into consideration your feedback and we will consider covering such scenarios in a future version.

Currently you could try the following workaround which will keep only one item selected

GridViewDataControl lastSelectionControl;
  
public Window1()
{
    InitializeComponent();
  
    this.clubsGrid.ItemsSource = Club.GetClubs();
    this.clubsGrid.SelectionChanged += new System.EventHandler<SelectionChangeEventArgs>(clubsGrid_SelectionChanged);
  
}
  
private bool changingSelection;
  
void clubsGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    if (changingSelection)
        return;
  
    // get the control on which the actual selection was performed
    var currentSelectionControl = e.OriginalSource as GridViewDataControl;
  
    this.changingSelection = true;
  
    if (currentSelectionControl != this.lastSelectionControl && 
        this.lastSelectionControl != null && 
        e.AddedItems.Count > 0)
    {
        this.lastSelectionControl.SelectedItems.Clear();
          
    }
  
    this.lastSelectionControl = currentSelectionControl;
    this.changingSelection = false;
}

Hope this helps.

Best wishes,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Cameron Molyneux
Top achievements
Rank 1
answered on 24 Feb 2010, 06:28 AM
that worked a treat.  thank you.

quick further question, is there a way to identify the id of the parent of a child table (when i say parent i mean upper most parent)

Thanks
0
Milan
Telerik team
answered on 24 Feb 2010, 02:35 PM
Hi Cameron Molyneux,

Could you please clarify a bit - I am not sure if I have understood the question correctly. Are you referring to the data property that represents the parent-child relation? When would you like to retrieve this information? 


Best wishes,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Cameron Molyneux
Top achievements
Rank 1
answered on 24 Feb 2010, 02:45 PM
What i mean is that i want to return the top parent row for whatever child i click on, regardless of how many levels of hierachy its down.

i.e if i select a row in a single nested table i want to find the parent row, but if i click a row in a table that is three levels down i want the parent.parent.parentrow ..

sorry if i'm not being clear
0
Milan
Telerik team
answered on 01 Mar 2010, 09:21 AM
Hi Cameron Molyneux,

Thank you for the clarification. you can achieve that with the following code:

// required namespaces: Telerik.Windows.Controls; System.Linq;
  
void clubsGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    // control that initiated the selection (can be on any level) 
    var selectionDataControl = (GridViewDataControl)e.OriginalSource;
  
    // et the top-most row
    var masterRow = this.GetParentsOfType<GridViewRow>(selectionDataControl).LastOrDefault();
}
  
private IEnumerable<T> GetParentsOfType<T>(FrameworkElement element) where T : FrameworkElement
{
    FrameworkElement currentElement = element;
    T parent = null;
    List<T> parents = new List<T>();
  
    while ((parent = currentElement.ParentOfType<T>()) != null)
    {
        parents.Add(parent);
        currentElement = parent;
    }
  
    return parents;
}


Greetings,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Cameron Molyneux
Top achievements
Rank 1
Answers by
Milan
Telerik team
Cameron Molyneux
Top achievements
Rank 1
Share this question
or