Syncing selection between two gridviews

3 posts, 1 answers
  1. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 08 Jun 2011 Link to this post

    I have two gridviews that are bound to the same QueryableCollectionView. I need the selected items of the two gridviews to be the same at all times.

     

    There seem to be some built-in funtionalty like this. If a select a row in GridView1 the same row will be selected in GridView2. But if I select multiple rows in GridView1, only the first selected of those rows will be selected in GridView2.

     

    How can I sync the selection between the gridviews?

    I used to have the gridviews bound to the same RadObservableCollection. Then I synced the selection manually on the SelectionChanged-event on GridView1 like this:

    private void GridView1_SelectionChanged(object sender, SelectionChangeEventArgs e)
    {
        GridView2.SelectedItems.Clear();
        foreach (var item in GridView1.SelectedItems)
        {
            GridView2.SelectedItems.Add(item);
        }
    }
    That worked fine, but I doesn't work now when I've switched to using QueryableCollectionView. When I clear the selected items of GridView2 the selected items of GridView1 will also be cleared. So it seems to be some built-in sync here (I'm guessing the QueryableCollectionView keeps track of selected items), but not like I need it to work

  2. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 08 Jun 2011 Link to this post

    Hi haagel,

    The QCV will handle only one item . If you need to handle multiple selection , you will need to implement it manually.

    As a starting point - you will need to subscribe to the CollectionChanged event of the SelectedItems property of RadGirdView. Inside the handler you will need to update the selection in the second grid.


    Best wishes,
    Pavel Pavlov
    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 09 Jun 2011 Link to this post

    Ok, I see.

    This is the code I came up with. It seems to work:

    /// <summary>
    /// This method executes when the selection in GridView1 is changed.
    /// Makes sure the selected items in GridView2 are the same as in GridView1.
    /// </summary>
    private void GridView1_SelectionChanged(object sender, SelectionChangeEventArgs e)
    {
        //Deselect the selected items in GridView2 that are not selected in GridView1.
        for (int i =GridView2.SelectedItems.Count-1; i >= 0; i--)
        {
             var item = GridView2.SelectedItems[i];

            //This item is selected in GridView2. If the item is not selected in GridView1...
            if (!GridView1.SelectedItems.Contains(item))
            {
                //...deselect the item in GridView2.
                GridView2.SelectedItems.Remove(item);
            }
        }
         
        //Select the items in GridView2 that are seleted in GridView1.
        foreach (var item in GridView1.SelectedItems)
        {
            //This item is selected in GridView1. If the item is not already selected in GridView2...
            if (!GridView2.SelectedItems.Contains(item))
            {
                //...select the item in GridView2.
                GridView2.SelectedItems.Add(item);
            }
        }
    }
Back to Top