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

Programmatic Sorting

3 Answers 54 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 21 Aug 2019, 04:14 AM

Hi,

I can successfully provide an initial sort order for my VirtualGrid using this code in the code behind file, as prescribed by your documentation:

var sortDescriptor = this.VirtualGrid.GetSortDescriptorForColumn(0);
sortDescriptor.SortDirection = System.ComponentModel.ListSortDirection.Ascending;

sortDescriptor.IsActive = true;

However, for reasons beyond the scope of this issue, my source data (an observable collection) and my custom data provider are created in my view model. My custom data provider is bound to the VirtualGrid via XAML.

Therefore, any attempt to apply your sorting code in the constructor of the code behind file fails because both the DataContext and DataProvider properties are not yet assigned in that instant (they are null), i.e. they are not populated until the View Model constructor files.

I can achieve what I want by placing the aforementioned code in the VirtualGrid's Loaded event in the code-behind file, but as a Newbie, I am concerned whether this is the correct solution.

Wayne

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 23 Aug 2019, 10:08 AM

Hi Wayne,

I've prepared a small sample project where placing the code you provided in the code-behind's constructor works as expected when initializing the source data and the data provider in the viewmodel's constructor. Please have a look and let me know how this differs from the setup at your end.

Nonetheless, I see no reason why you cannot apply the sort descriptor in the Loaded event of the control if this works for you. You can do so in an attached behavior or via the EventToCommandBehavior if you're concerned about breaking the MVVM pattern.

Please let me know about your thoughts on this.

Regards, Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
n/a
Top achievements
Rank 1
answered on 24 Aug 2019, 04:52 AM

Hi Dilyan,

Yes, this matches what I did. That at least confirms that I've handled it correctly, which is important for us newbies.

I just finished using your EventToCommandBehavior class in another module, so thanks for pointing that out, as I now feel aware of all my proper options.

Wayne

0
Dilyan Traykov
Telerik team
answered on 26 Aug 2019, 08:12 AM

Hi Wayne,

I'm happy to hear that you found the provided information helpful. Should any further questions or concerns arise, do not hesitate to contact me again.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
VirtualGrid
Asked by
n/a
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
n/a
Top achievements
Rank 1
Share this question
or