We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

SfDataGrid Loses sort order when an element is added to its binded collection

Thread ID:

Created:

Updated:

Platform:

Replies:

119324 Jun 4,2015 07:07 PM Sep 30,2017 11:56 PM WPF 5
loading
Tags: SfDataGrid
Soumyajit Roy
Asked On June 4, 2015 07:07 PM

Hi,

I have 2 Grids [SfDataGrid]. User clicks some Add button to add record(s) from one grid to another.  The grids are bound to two collections. The target grid is already sorted on an ID column. When a record is added to the target grid, the target grid loses the sort order, i.e. the newly added record does get inserted in the right place. 
In order to retain the sort order on the target grid after a record is added, I have written an event handler on the View object of the target grid [DataGrid.View.SourceCollectionChanged += View_SourceCollectionChanged]. In this event handler, I clear the SortColumnDescriptions collection and re add the Sort Item again. 

SortColumnDescriptions.Clear();
SortColumnDescription sortItem = new SortColumnDescription();
sortItem.ColumnName = ColumnName;
sortItem.SortDirection = System.ComponentModel.ListSortDirection.Ascending;;
SortColumnDescriptions.Add(sortItem);

The above code sorts the records on the target grid after a new record is added to its ItemSource ollection. But the problem is, the last added record always appears twice on the target grid. Even if I add multiple records, its only the last added record which appears twice and rest of the records are displayed in the proper sort order.
In the debugger, I checked the Records collection of the View object on the target grid, the DataGrid.View.Records shows the records properly [in the proper sort order], i.e. it does not show any record twice. I am baffled where does this extra record come from? 

Am I missing any IsSynchronizedWithCurrentItem kind of property?

Any suggestion?

Thanks,
Soumyajit Roy

Akila Rajaram [Syncfusion]
Replied On June 5, 2015 08:47 AM

Hi Soumyajit ,

Thank you for using Syncfusion Products.

You can directly  apply sorting for newly added record in button click by setting LiveDataUpdateMode as AllowDataShapping and there is no need to use the SourceCollectionChanged event which is specified in your  update  .We have prepared the sample based on your requirement . Please find the sample from the following location ,

Sample : http://www.syncfusion.com/downloads/support/forum/119324/ze/SfDataGrid_Sample_119324222495554

Please refer the following code example :

XAML :

<syncfusion:SfDataGrid x:Name="grid"

                               Grid.Row="0"

                               AllowSorting="True"                                                          

                               AutoGenerateColumns="True"

                               AutoGenerateColumnsMode="None"

                               ItemsSource="{Binding Stud}"

                               LiveDataUpdateMode="AllowDataShaping"
                               SelectionMode="Single">




Please refer the below ug link for more information about data manipulation in SfDataGrid,

Link : http://help.syncfusion.com/ug/wpf/index.html#!Documents/datamanipulation.htm

Please let us know if you require further assistance on this .

Regards,
Akila R.

Soumyajit Roy
Replied On June 5, 2015 11:57 AM

Thank You Akila. The solution is so effective, lot less code and clean.

Saranya CJ [Syncfusion]
Replied On June 8, 2015 01:30 AM

Hi Soumyajit,

Thank you for your update. Please let us know if you require any other assistance.

Regards,
Saranya


xudongli
Replied On September 28, 2017 09:13 AM

this doesn't work for me when I have customised sorting class


Muthukumar Kalyanasundaram [Syncfusion]
Replied On September 30, 2017 11:56 PM

Hi Xudongli, 

Thank you for your update. 

We are unable to reproduce the reported “Custom sorting is not worked properly while adding item in grid” issue in our end. Could you please share your xaml and customized sorting class files along with the model details if you did any other customization in grid?.  We could analyze further and resolve your reported issue at the earliest. 
 
Regards, 
Muthukumar K 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;