Grid not sorting after new data is bound

15 posts, 0 answers
  1. Lee
    Lee avatar
    10 posts
    Member since:
    Jan 2013

    Posted 12 Apr 2013 Link to this post

    I have a gridview on a control.  I have set the grid to have a datasource relative to a property on the control:

    <telerik:RadGridView Grid.Row="1" Name="adjustments" CanUserDeleteRows="False" CanUserInsertRows="False" CanUserResizeRows="False" ShowGroupPanel="False" SelectionChanged="adjustments_SelectionChanged"
            ItemsSource="{Binding Path=CurrentClaim.claimAdjustments, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" AutoGenerateColumns="False" IsReadOnly="True" MouseDoubleClick="adjustments_MouseDoubleClick">
    <telerik:RadGridView.Columns>
          <telerik:GridViewDataColumn Name="colCreatedOn" DataMemberBinding="{Binding createdOn}" Header="Posting Date" DataFormatString="{}{0:MM/dd/yyyy}" SortingState="Descending"  />
          <telerik:GridViewDataColumn DataMemberBinding="{Binding claimAdjustmentReason.description}" Header="Reason" />
    </telerik:RadGridView.Columns>
    </telerik:RadGridView>

    The property CurrentClaim does notify the grid and the grid does update the data when the new claim is set in CurrentClaim.  The problem is that the grid is sorted based on the first column, but when new data is loaded into the grid it is not sorted by this column.  Can someone please tell me what I am doing wrong or why the sort doesn't work when new data is loaded?

    -Lee


  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 16 Apr 2013 Link to this post

    Hello Lee,

    Generally the Sorting will be refreshed when a CollectionChanged event is raised from the bound source collection. May I ask you to share how is your ItemsSource defined? How do you load the new data?

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Lee
    Lee avatar
    10 posts
    Member since:
    Jan 2013

    Posted 16 Apr 2013 Link to this post

    I have a dependency property on my control that exposes an entity framework object with a collection on it.  So on my control I have:

    public static readonly DependencyProperty CurrentClaimProperty = DependencyProperty.Register("CurrentClaim", typeof(claim), typeof(ClaimData), new PropertyMetadata());
     
    public claim CurrentClaim
    {
         get { return (claim)GetValue(CurrentClaimProperty); }
         set { SetValue(CurrentClaimProperty, value); }
    }

    As you can see in the XAML I posted earlier, this is linked to the grid control.  The grid is loaded from the CurrentClaim.claimAdjustments collection.  This all seems to work just fine.  The only problem is that I have set the grid to sort descending on the date column every time but the grid seems to loose that when a new CurrentClaim is set.  It does refresh the claimAdjustments, just does not sort them.

  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 Apr 2013 Link to this post

    Hello,

    Would you please set the ItemsSource to null, and then set it to the new data to be bound?

    Greetings,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Lee
    Lee avatar
    10 posts
    Member since:
    Jan 2013

    Posted 17 Apr 2013 Link to this post

    So don't bind the data through WPF, instead bind it in code behind.  Is that what you are recommending?
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 18 Apr 2013 Link to this post

    Hello,

    It seems that there is not raised a notification as you refresh the data, the claimAdjustments.

    You have three options:
    1. Raise a CollectionChanged when the bound collection is changed.
    2. Invoke a Rebind() for the RadGridView.
    3. Reset the ItemsSource of the RadGridView.

    I hope this helps.

    Regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Sandeep
    Sandeep avatar
    9 posts
    Member since:
    Dec 2012

    Posted 18 Apr 2013 Link to this post

    Hi,

    We are facing the same issue in Grid sorting not working on refresh. I tried setting the Grid itemsource to null in Viewmodel and the raised propertychanged followed by binding the grid to correct value but sorting applied in XAML is not working.

     

    <controls2:RadGridView.SortDescriptors>

     

    <telerik:ColumnSortDescriptor Column="{Binding Columns[\MyCustomOrder\], ElementName=MyGridName}"

     

    SortDirection="Ascending" />

     

    </controls2:RadGridView.SortDescriptors>


    Thanks,
    SK
  9. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Apr 2013 Link to this post

    Hello,

    Does it work the first time the GridView is loaded? Does invoking a RadGridView.Rebind() re-sort the data? If not, then would it be possible for you to isolate the issue in a sample project and send it to us?

    Regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  10. Anthony
    Anthony avatar
    58 posts
    Member since:
    Aug 2011

    Posted 11 Nov 2014 in reply to Dimitrina Link to this post

    Hi,

    I hit this issue today.

    If the Sort is set in XAML and the data refreshed the grid is no longer sorted.

    Doing a Rebind after the data is refreshed does not help either.

    However if you sort the grid using the mouse and then refresh the data the grid maintains the correct sorting.

    I really want my data to be sorted a certain way for the user when they open the window rather them having to click on the column.

    Thanks
    Anthony
  11. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 12 Nov 2014 Link to this post

    Hello Anthony,

    You say that the issue occurs after refreshing the bound data. Would you please share some more information how do you do so?

    When sorting with the user clicking on the column header, then a ColumnSortDescriptor is added to RadGridView.SortDescriptors collection. You can try defining such in XAML.
    For example:
    <telerik:RadGridView.SortDescriptors>
          <telerik:ColumnSortDescriptor Column="{Binding Columns[\Title\], ElementName=radGridView}" SortDirection="Ascending"/>
    </telerik:RadGridView.SortDescriptors>

    Does it work fine that way?

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  12. Anthony
    Anthony avatar
    58 posts
    Member since:
    Aug 2011

    Posted 12 Nov 2014 in reply to Dimitrina Link to this post

    Hi,

    I am refreshing the data from my ViewModel, which the ItemSource of the RadGridView is bound to. Initially it would be NULL until the data is loaded from the database.

    My XAML looks like this:

    <telerik:RadGridView x:Name="RadGridView" ItemsSource="{Binding Data}">

    And my ViewModel does this after the data is loaded from the database. Data is null previously:

    Data = new ObservableCollection<EmployeeWork>(newList);

    I have tried setting the initial ColumnSortDescriptor in XAML as per your example and that does not help.

    Please note that it also works OK if I load the RadGridView settings from disk after persisting its layout when exiting the application. So it looks like it is only the initial sorting set in XAML that does not work.

    Thanks
    Anthony
  13. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 Nov 2014 Link to this post

    Hi Anthony,

    Basically, the defined sorting descriptors will be cleared on reset of the ItemsSource. If this is not the case, may I ask you to open a new support ticket for product RadGridView and send us a demo project illustrating the problem? You can take a look at this blog post for a reference on how to isolate an issue. 

    That way I will check it locally and suggest a solution.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  14. Anthony
    Anthony avatar
    58 posts
    Member since:
    Aug 2011

    Posted 13 Nov 2014 in reply to Dimitrina Link to this post

    Hi,

    I am not sure if your first sentence in the above response is summarizing the issue or telling me that is the designed behaviour?

    Also the link in your post is not valid.

    Thanks
    Anthony

  15. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 14 Nov 2014 Link to this post

    Hello Anthony,

    I meant that by design the defined SortDescriptors will be cleared if you reset the ItemsSource after setting it to null first and then setting it again. As I understand in your case you refresh it filling data and not resetting it. So then the descriptors should not be cleared. Another approach I can suggest you as to defining a SortDescriptor initially would be to subscribe for the DataLoaded event of RadGridView and add the desired descriptor there.

    As to the link, it indeed seems to be not active any more. You can find the correct one here:
    http://blogs.telerik.com/aspnet-ajax/posts/10-09-29/isolating-a-problem-in-a-sample-project.aspx

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  16. Anthony
    Anthony avatar
    58 posts
    Member since:
    Aug 2011

    Posted 17 Nov 2014 in reply to Dimitrina Link to this post

    Hi,

    I managed to work around the issue. I am now setting the default Sort Descriptor in my ViewModel after I set the Data. It's ugly but it works.

    Thanks
    Anthony

Back to Top
UI for WPF is Visual Studio 2017 Ready