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

GridViewSelectColumn + paging

8 Answers 197 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 29 Mar 2010, 04:18 PM
Hi,

I'm using a grid, bound to an ienumerable using the standard 'select/deselect all' option:

<telerikGridView:GridViewSelectColumn UniqueName="columnSelectItem" Width="Auto">
                                            <telerikGridView:GridViewSelectColumn.Header>
                                                <CheckBox Click="CheckBox_Click" />
                                            </telerikGridView:GridViewSelectColumn.Header>
                                        </telerikGridView:GridViewSelectColumn>

I'm also using paging as described here: http://demos.telerik.com/silverlight/#GridView/PagingIEnumerable

Paging works ok, and so does select / deselect all. However, clicking the checkbox in the header to select / deselect all only operates on the items being displayed, not on the whole list. Is this by design? If so, is there a workaround?

Thanks, James



8 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 30 Mar 2010, 03:47 PM
Hi James,

This behavior is expected since the grid only has access to the items that are displayed in the current page. 

Here is one workaround that might work for your scenario:

01.BitArray selectedItemsFlags; 
02.bool selectionRestored = false;
03.// subscribe for this.myGrid.Items.PageChanging | this.myGrid.Items.PageChanged
04.  
05.void Items_PageChanged(object sender, System.EventArgs e)
06.{
07.    this.myGrid.SelectedItems.Clear(); 
08.    this.Dispatcher.BeginInvoke(new System.Action(() => this.RestoreSelectionAfterPageChange()));
09.}
10.  
11.void Items_PageChanging(object sender, System.ComponentModel.PageChangingEventArgs e)
12.{
13.    // save current selection
14.    this.selectedItemsFlags = new BitArray(this.myGrid.Items.PageSize); 
15.  
16.    foreach (var item in this.myGrid.SelectedItems) 
17.    {
18.        var index = this.myGrid.Items.IndexOf(item); 
19.        this.selectedItemsFlags[index] = true;
20.    }
21.  
22.    this.selectionRestored = false;
23.}
24.  
25.private void RestoreSelectionAfterPageChange()
26.{
27.    if (this.selectedItemsFlags == null || this.selectionRestored)
28.        return;
29.  
30.    // restore selection
31.    for (int i = 0; i < this.selectedItemsFlags.Count; i++)
32.    {
33.        var isSelected = this.selectedItemsFlags[i];
34.  
35.        if (isSelected && i < this.myGrid.Items.Count) 
36.            this.myGrid.SelectedItems.Add(this.myGrid.Items[i]); 
37.    }
38.  
39.    this.selectionRestored = true;
40.}


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.
0
James
Top achievements
Rank 1
answered on 01 Apr 2010, 10:14 PM
Hi Milan,

Thanks for your answer. It looks as though we'll be just as well off having an IsSelected property in our object to bind to a standard column and handle remembering selection there. Is there a simple way of putting a checkbox in the header of a GridViewDataColumn? We can then handle its events ourselves.

Thanks for your help, James.

btw, when you check the checkbox on a GridViewSelectColumn and change page, the checkbox stays checked, even though all the records on the next page are unchecked.
0
James
Top achievements
Rank 1
answered on 02 Apr 2010, 07:50 PM
Don't worry - I found how to do it (and then changed back to SelectColumn anyway!)
Thanks, james.
0
Claire Kaplan
Top achievements
Rank 1
answered on 05 Apr 2010, 05:28 PM
This is a strange coincidence, but we recently ran into the same kind of problems that you did. Could you summarize how you managed to retain selections across pages? I was tempted to mimic my own gridview select-column but it seems like you were able to overcome most issues with it. Also, how did you ensure the select-all didn't remain checked when you navigate to the next page?

Thanks,
-Avinash Chugh
0
James
Top achievements
Rank 1
answered on 14 Apr 2010, 02:57 PM
Hi Claire,

I'm afraid we never solved the problem of the checkbox remaining checked between pages. We retained selection by having a 'selected' property on the object that we bound to. It was hard work and didn't work perfectly, so we went back to the default method described in this thread using selectcolumns. This isn't perfect either, but it's good enough for now.

Sorry I can't help more.

James.
0
Shreya
Top achievements
Rank 1
answered on 20 Nov 2014, 03:22 PM
Hi James and Maya,
I am facing similar kind of problem. Please Help me in achieving following scenarios:

1) When Select All suppose I have 186 records and my page size is 10 so I have 19 pages in raddatapager and last one has only 6 records. I have tried implementing the above logic by Maya, when I go to my last page no 19 and then again clicking on any page it shows me only first 6 rows selected not all. 

2) To capture my selecteditems I have attached a behaviour to grid to get the selected Items in my view model. There I am only getting the first page selected Items
((CAManagementViewModel)this.AssociatedObject.DataContext).SelectedVehicles = this.AssociatedObject.SelectedItems;

3) In the above property of selected Items if user selects different records on different page how to get all maintained in SelectedItems of
grid.

Thanks a lot in advance



















0
James
Top achievements
Rank 1
answered on 20 Nov 2014, 04:41 PM
Sorry Shreya, it's four years since I looked at this (and other members of the team do more coding on this than me now), and I don't think any knowledge of this remains in my long-term memory!

Hope you get it sorted...
0
Shreya
Top achievements
Rank 1
answered on 21 Nov 2014, 08:58 AM
Hi Milan,
Can you please help me in this...

I am facing similar kind of problem. Please Help me in achieving following scenarios:

1) When Select All suppose I have 186 records and my page size is 10 so I have 19 pages in raddatapager and last one has only 6 records. I have tried implementing the above logic by You, when I go to my last page no 19 and then again clicking on any page it shows me only first 6 rows selected not all. 

2) To capture my selecteditems I have attached a behaviour to grid to get the selected Items in my view model. There I am only getting the first page selected Items
((CAManagementViewModel)this.AssociatedObject.DataContext).SelectedVehicles = this.AssociatedObject.SelectedItems;

3) In the above property of selected Items if user selects different records on different page how to get all maintained in SelectedItems of
grid.

Thanks a lot in advance
Tags
GridView
Asked by
James
Top achievements
Rank 1
Answers by
Milan
Telerik team
James
Top achievements
Rank 1
Claire Kaplan
Top achievements
Rank 1
Shreya
Top achievements
Rank 1
Share this question
or