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

[Solved] Paging through RadGridView clears SelectedItems property

2 Answers 171 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris Jones
Top achievements
Rank 1
Chris Jones asked on 06 Dec 2010, 03:46 PM
Currently upgrading from 2010 Q1 release for SL3 to 2010 Q3 release for SL4 and one of the pieces of functionality we were using no longer works as expected.  When displaying a RadGridView, paging with a RadDataPager (server side paging), with WCF Ria, in SL3 our users were able to select multiple items on a page, then go to the next page, select more items, etc, and when the user was ready to save their selections, all selected items from all pages were contained in the SelectedItems property of the RadGridView.  This no longer occurs with the the latest SL4 version.  As soon as the next page is selected, the SelectedItems property of the RadGridView is cleared of all selections.

Is there a property that I am missing that needs to be set to make this functionality continue to work the same?  Is this something that wasnt ported currectly from SL3 to SL4?

Thanks
Chris

2 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 06 Dec 2010, 04:27 PM
Hello Chris Jones,

I spoke with the developers and it turns out that though is sounds strange the previous behavior was caused by a bug in our selection mechanism - items that did not exist in the Items collection were contained in the SelectedItems collection, which was not correct. 

We are currently evaluating possible ways to support such selection but it is still unclear when we will be able to introduce such support. 

Please find attached a sample project here. It shows how to preserve the selected items during the navigation between different pages.



Regards,
Veselin Vasilev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Chris Jones
Top achievements
Rank 1
answered on 09 Dec 2010, 06:07 PM
Here is the solution I came up with.  This was the least intrusive to the current logic of our applications.

/// <summary>
/// Behavior to allow for selection of objects and retaining those selections
/// across pages during paging.  Only works for RadGridView where SelectionMode
/// is not set to Single
/// </summary>
public class AllowSelectionAcrossPages : Behavior<Telerik.Windows.Controls.RadGridView>
{
    private bool justAddedRemovedItemsFromPaging;
    bool justPaged;
 
    #region OnAttached
    protected override void OnAttached()
    {
        AssociatedObject.Loaded += AssociatedObject_Loaded;
    }
    #endregion
    #region OnDetaching
    protected override void OnDetaching()
    {
        if (AssociatedObject.SelectionMode == System.Windows.Controls.SelectionMode.Single)
        {
            // Do nothing
        }
        else
        {
            AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
            AssociatedObject.Items.PageChanging -= Items_PageChanging;
        }
    }
    #endregion
 
    #region AssociatedObject_Loaded
    void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        AssociatedObject.Loaded -= AssociatedObject_Loaded;
 
        if (AssociatedObject.SelectionMode == System.Windows.Controls.SelectionMode.Single)
        {
            // Do nothing
        }
        else
        {
            AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
            AssociatedObject.Items.PageChanging += Items_PageChanging;
 
            foreach (GridViewSelectColumn column in AssociatedObject.Columns.Where<Telerik.Windows.Controls.GridViewColumn>(c => c is GridViewSelectColumn))
            {
                CheckBox headerCheckBox = new CheckBox { IsThreeState = false };
                headerCheckBox.Click += headerCheckBox_Click;
 
                // Tooltip on headerCheckBox to show current number of selected items across all pages
                TextBlock tooltipTextblock = new TextBlock();
                tooltipTextblock.SetBinding(TextBlock.TextProperty, new Binding("Count") { Source = AssociatedObject.SelectedItems });
                ToolTip tooltip = new ToolTip { Placement = System.Windows.Controls.Primitives.PlacementMode.Right, Content = tooltipTextblock };
                ToolTipService.SetToolTip(headerCheckBox, tooltip);
 
                // Border is needed to ensure no Telerik processing on the checkbox in the header of the GridViewSelectColumn
                column.Header = new Border() { Child = headerCheckBox };
            }
        }
    }
    #endregion
    #region AssociatedObject_SelectionChanged
    void AssociatedObject_SelectionChanged(object sender, SelectionChangeEventArgs e)
    {
        // Needed to get around Telerik processing to remove all items
        // from the SelectedItems collection when performing paging
        if (justPaged || justAddedRemovedItemsFromPaging)
        {
            justPaged = false;
 
            if (justAddedRemovedItemsFromPaging)
            {
                justAddedRemovedItemsFromPaging = false;
            }
            else
            {
                justAddedRemovedItemsFromPaging = true;
            }
 
            foreach (var item in e.RemovedItems)
            {
                if (!AssociatedObject.Items.Contains(item))
                {
                    AssociatedObject.SelectedItems.Add(item);
                    justAddedRemovedItemsFromPaging = true;
                }
            }
        }
 
        int currentlyselectedonpagecount = 0;
        foreach (var item in AssociatedObject.SelectedItems)
        {
            if (AssociatedObject.Items.Contains(item))
            {
                currentlyselectedonpagecount++;
            }
        }
 
        // Processing to check the header checkbox if all items on the current page have been selected.
        foreach (GridViewSelectColumn column in AssociatedObject.Columns.Where<Telerik.Windows.Controls.GridViewColumn>(c => c is GridViewSelectColumn))
        {
            if (column.Header is UIElement)
            {
                foreach (CheckBox checkbox in (column.Header as UIElement).ChildrenOfType<CheckBox>())
                {
                    if (currentlyselectedonpagecount == AssociatedObject.Items.Count)
                    {
                        checkbox.IsChecked = true;
                    }
                    else
                    {
                        checkbox.IsChecked = false;
                    }
                }
            }
        }
    }
    #endregion
    #region Items_PageChanging
    void Items_PageChanging(object sender, System.ComponentModel.PageChangingEventArgs e)
    {
        justPaged = true;
    }
    #endregion
    #region headerCheckBox_Click
    void headerCheckBox_Click(object sender, RoutedEventArgs e)
    {
        if ((sender as CheckBox).IsChecked.GetValueOrDefault())
        {
            SelectAllItemsOnPage();
        }
        else
        {
            RemoveAllItemsOnPage();
        }
    }
    #endregion
    #region RemoveAllItemsOnPage
    private void RemoveAllItemsOnPage()
    {
        foreach (var item in AssociatedObject.Items)
        {
            if (AssociatedObject.SelectedItems.Contains(item))
            {
                AssociatedObject.SelectedItems.Remove(item);
            }
        }
    }
    #endregion
    #region SelectAllItemsOnPage
    private void SelectAllItemsOnPage()
    {
        foreach (var item in AssociatedObject.Items)
        {
            if (!AssociatedObject.SelectedItems.Contains(item))
            {
                AssociatedObject.SelectedItems.Add(item);
            }
        }
    }
    #endregion
}
Tags
GridView
Asked by
Chris Jones
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Chris Jones
Top achievements
Rank 1
Share this question
or