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

Grid not sorting after new data is bound

14 Answers 1095 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 12 Apr 2013, 02:58 PM
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


14 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 16 Apr 2013, 10:36 AM
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.

0
Lee
Top achievements
Rank 1
answered on 16 Apr 2013, 03:39 PM
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.

0
Dimitrina
Telerik team
answered on 17 Apr 2013, 08:52 AM
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.

0
Lee
Top achievements
Rank 1
answered on 17 Apr 2013, 06:46 PM
So don't bind the data through WPF, instead bind it in code behind.  Is that what you are recommending?
0
Dimitrina
Telerik team
answered on 18 Apr 2013, 01:25 PM
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.

0
Sandeep
Top achievements
Rank 1
answered on 18 Apr 2013, 10:09 PM
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
0
Dimitrina
Telerik team
answered on 19 Apr 2013, 12:50 PM
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.

0
Anthony
Top achievements
Rank 1
Veteran
answered on 12 Nov 2014, 05:29 AM
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
0
Dimitrina
Telerik team
answered on 12 Nov 2014, 09:16 AM
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.

 
0
Anthony
Top achievements
Rank 1
Veteran
answered on 12 Nov 2014, 09:28 PM
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
0
Dimitrina
Telerik team
answered on 13 Nov 2014, 02:50 PM
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.

 
0
Anthony
Top achievements
Rank 1
Veteran
answered on 13 Nov 2014, 09:34 PM
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

​
0
Dimitrina
Telerik team
answered on 14 Nov 2014, 07:42 AM
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.

 
0
Anthony
Top achievements
Rank 1
Veteran
answered on 17 Nov 2014, 11:58 PM
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

Tags
GridView
Asked by
Lee
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Lee
Top achievements
Rank 1
Sandeep
Top achievements
Rank 1
Anthony
Top achievements
Rank 1
Veteran
Share this question
or