Retain Pinned status on items in GridView

1 Answer 114 Views
GridView
Martin
Top achievements
Rank 2
Iron
Iron
Iron
Martin asked on 19 Jan 2022, 02:16 PM

I'm scratching my head deep over this one.. 

GridView has a GridViewPinRowColumn which out of the box looks promising: it simply enables pinning of items to the top -- YAY !

BUT - i need to be able to refresh the grid with new instances of the data every now an then, so my mission is to retain the status of pinned items.

so i found this StyleStter which binds to a property on the item. 

 <Style TargetType="telerik:GridViewRow">
        <Setter Property="IsPinned" Value="{Binding IsPinned, Mode=TwoWay}" />
 </Style>

There are SOME issues with this :

1. When the GridView renders the items, it resets IsPinned to FALSE !

2. GridView doesn't have an event that fires when all items are rendered !

Let's look at issue 1:

NotifyPropertyChanged turns this into a nightmare, because, when the GridView renders the items, it resets IsPinned, which effectively negates the option to store the value either on the item itself or an external list..

Issue 2: 

Even IF is could store a list of IsPinned items, while reloading the Grid, there is no event that fires when items are finished rendering. Thereby making it more or less impossible to restore pinned status 

 

One solution would require a change request: Dont reset IsPinned when rendering the items

Or - do you have another solution to this issue ? 

 

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Stenly
Telerik team
answered on 24 Jan 2022, 09:30 AM

Hello Martin,

To achieve the wanted result, when refreshing the RadGridView control instance, you could cache the data items, which have the property, bound to the IsPinned property of the GridViewRow element, set to True. Then, the TogglePinnedRowState command could be used, which will take as a parameter the item that needs to be pinned, and the target RadGridView control to pin the item to.

The following code snippet refreshes the RadGridView element instance inside a button's Click event, and pins the rows, which were previously in a pinned state, by utilizing the TogglePinnedRowState command:

private void RadButton_Click(object sender, RoutedEventArgs e)
{
    var context = (MyViewModel)(this.gridView.DataContext);
    //cache the items in Pinned state
    var cachedPinnedContext = context.Clubs.Where(x => x.IsPinned).ToList();

    var togglePinnedStateCommand = RadGridViewCommands.TogglePinnedRowState as RoutedUICommand;
   
    //refresh the RadGridView control instance's data
    this.gridView.ItemsSource = null;
    this.gridView.ItemsSource = context.Clubs;

    foreach (var item in context.Clubs)
    {
        if (cachedPinnedContext.Contains(item))
        {
            //re-pin the needed rows
            togglePinnedStateCommand.Execute(item, this.gridView);
        }
    }
}

With that said, I have prepared a sample project for you to test, so, could you give it a try?

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Martin
Top achievements
Rank 2
Iron
Iron
Iron
commented on 25 Jan 2022, 08:29 AM

Thanks man - your sample works !

Didn't think that firing togglePinnedStateCommand right after populating the ItemsSource would work, because the rendering is async.

I'll implement it in my project when i'm back on it.

Martin
Top achievements
Rank 2
Iron
Iron
Iron
commented on 26 Jan 2022, 10:52 AM | edited

Just an update - I've implemented the code you attached and it works !

Though - i had to add a Dispatcher:

Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
{
     var togglePinnedStateCommand = RadGridViewCommands.TogglePinnedRowState as RoutedUICommand;

     foreach (var item in Variants)
     {
           togglePinnedStateCommand.Execute(item, radgrid);
     }
}));
Stenly
Telerik team
commented on 26 Jan 2022, 11:12 AM

I am happy to hear that the proposed approach is a suitable solution for your current scenario. Let me know if you need any further assistance regarding this matter.
Tags
GridView
Asked by
Martin
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or