Jan
1
Written by:
Michael Washington
1/1/2012 10:56 AM
When you have a list of items that have a particular sort order, you may have the need to allow the end-user the ability to change the sort order. This article shows a solution.
Tasks Table
We start off with a Task table.
We override the Task_Created event to set a default value for SortOrder since it is a required field:
partial void Task_Created()
{
// set the sort order
this.SortOrder = 0;
}
We also override the Tasks_Inserted method to set the sort order after the record is inserted:
partial void Tasks_Inserted(Task entity)
{
// Get all saved Tasks
var colTasks = this.DataWorkspace.ApplicationData.Tasks.GetQuery().Execute();
if (colTasks.Count() > 0)
{
// Set sort order to the highest found plus one
entity.SortOrder =
colTasks.OrderByDescending(x => x.SortOrder).FirstOrDefault().SortOrder + 1;
}
else
{
// set the sort order to number one
entity.SortOrder = 1;
}
}
Create The Screen
We create a Screen with the Tasks collection and select Edit Query.
We set the SortOrder for the collection.
Running The Application
When we run the application, we see that the default value is set for each record…
…but when we click Save, the records will now have an incremental sort value.
Up And Down Buttons
Next, we click on the Command Bar of the Data Grid Row, and add a Table Layout Group, and insert Up and Down buttons.
The following is the code used:
partial void Up_Execute()
{
var SelectedTask = Tasks.SelectedItem;
int intSelectedTaskSortOrder = SelectedTask.SortOrder;
// Determine if there is a Task with a SortOrder
// that is one less than the current one
var TaskWithLowerSortOrder = (from Task in Tasks
where Task.SortOrder < intSelectedTaskSortOrder
orderby Task.SortOrder descending
select Task).FirstOrDefault();
if (TaskWithLowerSortOrder != null)
{
int intTaskWithLowerSortOrder = TaskWithLowerSortOrder.SortOrder;
// Switch the SortIds
TaskWithLowerSortOrder.SortOrder = intSelectedTaskSortOrder;
SelectedTask.SortOrder = intTaskWithLowerSortOrder;
Save();
Tasks.Refresh();
Tasks.SelectedItem = null;
}
}
partial void Down_Execute()
{
var SelectedTask = Tasks.SelectedItem;
int intSelectedTaskSortOrder = SelectedTask.SortOrder;
// Determine if there is a Task with a SortOrder
// that is one higher than the current one
var TaskWithHigherSortOrder = (from Task in Tasks
where Task.SortOrder > intSelectedTaskSortOrder
orderby Task.SortOrder
select Task).FirstOrDefault();
if (TaskWithHigherSortOrder != null)
{
int intTaskWithHigherSortOrder = TaskWithHigherSortOrder.SortOrder;
// Switch the SortIds
TaskWithHigherSortOrder.SortOrder = intSelectedTaskSortOrder;
SelectedTask.SortOrder = intTaskWithHigherSortOrder;
Save();
Tasks.Refresh();
Tasks.SelectedItem = null;
}
}
When we run the application, we can change the sort order of the items using the buttons.
Download Code
The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx
5 comment(s) so far...
Another excellent article.
Thanks, Dave
By fishy on
1/3/2012 9:16 AM
|
@fishy - Thanks!
By Michael Washington on
1/3/2012 3:43 PM
|
Hi Micheal..
Is there a way to do the same WITHOUT performing a Save & Reloading (i.e. Refresh) everytime a list item is moved?
What i want to achieve is.. the user can reorder the list how many times they want and then, only perform the save once when they are absolutely satisfied with the ordering.
By Faris Wong on
1/4/2012 10:36 PM
|
@Faris Wong - Yes, you would need to use a Silverlight custom control.
By Michael Washington on
1/5/2012 5:16 AM
|
I was afraid you would say that..
Thanks anyway.. :)
By Faris Wong on
1/5/2012 7:14 PM
|