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

Syncing selection between two gridviews

2 Answers 220 Views
GridView
This is a migrated thread and some comments may be shown as answers.
haagel
Top achievements
Rank 1
haagel asked on 08 Jun 2011, 11:57 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Pavel Pavlov
Telerik team
answered on 08 Jun 2011, 01:44 PM
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
0
haagel
Top achievements
Rank 1
answered on 09 Jun 2011, 09:33 AM
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);
        }
    }
}
Tags
GridView
Asked by
haagel
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
haagel
Top achievements
Rank 1
Share this question
or