locked
Need help with manually ordering rows RRS feed

  • Question

  • I have a table of timesheet line items. 

    seqno, Employee, timesheetID, timeIn,TimeOut,LunchIn,LunchOut

    I have added this to a screen as a datagrid but need the ability to manually sort the rows, ie move a record up or down.

    I have the buttons created on the command bar and the following code setup. This works to a degree. I have 2 issues with it.

    1) The sort itself does not change until after a save and refresh. Can anything be done with this?

    2) If I try to move a record past the last record (>Max of SeqNo) or before the first (< 1) I get errors. How can I add a clause to check for the min/max values of a column in the if statement so the code doesn't run.

            partial void MoveUp_Execute()
            {
                // Moves the record up a row
                var selected = this.TimeSheetLines.SelectedItem;
                if (selected != null)
                {
                    var upStep = this.TimeSheetLines.First(r => r.SeqNo == (selected.SeqNo - 1));
                    upStep.SeqNo = selected.SeqNo;
                    selected.SeqNo = selected.SeqNo - 1;
                }
            }
    
            partial void MoveDown_Execute()
            {
                // Moves the selected record down a row
                var selected = this.TimeSheetLines.SelectedItem;
                if (selected != null)
                {
                    var upStep = this.TimeSheetLines.First(r => r.SeqNo == (selected.SeqNo + 1));
                    upStep.SeqNo = selected.SeqNo;
                    selected.SeqNo = selected.SeqNo + 1;
                }
            }

    If someone has a better idea on how to get this down, please I am all ears. To be fair I am just getting started with Lightswitch and C#.

    Thanks for any help

    Josh


    Thursday, March 14, 2013 2:59 PM

Answers

  • Hello

    I personally would add some code to the _CanExecute of each button that checks the current position of the selected row then based on this make the button available or not. So, for example, for your MoveUp button I would do something like.....

    partial void_MoveUp_CanExecute(ref bool result)
    {
         bool canMove = false;
         if(this.TimeSheetLines.SelectedItem != null){
               if(this.TimeSheetLines.SelectedItem.SeqNo !=1)
               {
                  canMove = true;
               }
         }
         result = canMove;
    }

    You would then just do something similar to the MoveDown button but instead of making sure that it is not equal to 1 you would be making sure that it is not equal to the last seqno available.

    As for the not showing the move until a save and refresh, why not do the save within the button action itself? If there are no other things on the page to save then just add this.Save(); and this.Refresh(); after you have updated the SeqNo.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Proposed as answer by Otis Ranger Monday, March 18, 2013 5:08 PM
    • Marked as answer by JoshWilliam Monday, March 18, 2013 6:40 PM
    Friday, March 15, 2013 4:42 PM

All replies

  • Hello

    I personally would add some code to the _CanExecute of each button that checks the current position of the selected row then based on this make the button available or not. So, for example, for your MoveUp button I would do something like.....

    partial void_MoveUp_CanExecute(ref bool result)
    {
         bool canMove = false;
         if(this.TimeSheetLines.SelectedItem != null){
               if(this.TimeSheetLines.SelectedItem.SeqNo !=1)
               {
                  canMove = true;
               }
         }
         result = canMove;
    }

    You would then just do something similar to the MoveDown button but instead of making sure that it is not equal to 1 you would be making sure that it is not equal to the last seqno available.

    As for the not showing the move until a save and refresh, why not do the save within the button action itself? If there are no other things on the page to save then just add this.Save(); and this.Refresh(); after you have updated the SeqNo.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Proposed as answer by Otis Ranger Monday, March 18, 2013 5:08 PM
    • Marked as answer by JoshWilliam Monday, March 18, 2013 6:40 PM
    Friday, March 15, 2013 4:42 PM
  • Otis,

      Thanks for the reply. I've made some good headway and can now order my records on insert, default my values, etc. I have one last hurdle, and maybe someone can point me in the right direction. I would really like to keep the functionality of being able to tab to a new record and default values but the code only executes when I use the add button I have. When tabbing to a new row I get nothing. Where am I going wrong? Here is the code I currently have.

            partial void TimeSheetLines_Changed(NotifyCollectionChangedEventArgs e)
            {
                this.TotalTimeSheetHours = this.TimeSheetLines.Sum((x) => x.Hours);
                if (this.TimeSheetLines.SelectedItem != null && this.TimeSheetLines.SelectedItem.Employee != null && this.TimeSheetLines.SelectedItem.EmployeeClassRate == null)
                {
                    this.TimeSheetLines.SelectedItem.EmployeeClassRate = this.TimeSheetLines.SelectedItem.Employee.classRate.ClassCode;
                }
            }
    
            partial void TimeSheetLines_SelectionChanged()
            {
                this.TotalTimeSheetHours = this.TimeSheetLines.Sum((x) => x.Hours);
                if (this.TimeSheetLines.SelectedItem != null && this.TimeSheetLines.SelectedItem.Employee != null &&this.TimeSheetLines.SelectedItem.EmployeeClassRate == null)
                {
                    this.TimeSheetLines.SelectedItem.EmployeeClassRate = this.TimeSheetLines.SelectedItem.Employee.classRate.ClassCode;
                }
    
            }
    
            partial void MoveUp_Execute()
            {
                // Moves the record up a row
                var selected = this.TimeSheetLines.SelectedItem;
                if (selected != null && selected.SeqNo != 1)
                {
                    var upStep = this.TimeSheetLines.First(r => r.SeqNo == (selected.SeqNo - 1));
                    upStep.SeqNo = selected.SeqNo;
                    selected.SeqNo = selected.SeqNo - 1;
                }
            }
    
            partial void MoveDown_Execute()
            {
                // Moves the selected record down a row
                var selected = this.TimeSheetLines.SelectedItem;
                int maxlist = this.TimeSheetLines.Count;
                if (selected != null && selected.SeqNo != maxlist)
                {
                    var upStep = this.TimeSheetLines.First(r => r.SeqNo == (selected.SeqNo + 1));
                    upStep.SeqNo = selected.SeqNo;
                    selected.SeqNo = selected.SeqNo + 1;
                }
    
            }
    
            partial void AddLine_Execute()
            {
                // Write your code here.
                this.TimeSheetLines.AddNew();
                this.TimeSheetLines.SelectedItem.SeqNo = this.TimeSheetLines.Count;
                this.TimeSheetLines.SelectedItem.EmployeeClassRate = this.TimeSheet.SelectedItem.classRate.ClassCode;
                
                
            }

    Thanks for everyone's help here!

    Josh

    Saturday, March 16, 2013 3:34 AM
  • You could disable that behaviour using 'Show Add-new Row' in the Data Grid properties or try to manipulate:

            partial void TimeSheetLines_Changed(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.NewItems != null)
                {
                    //this.ShowMessageBox(e.NewItems.Count.ToString());
                    var n = e.NewItems.Count;
                }
            }

    I am wary of the effect code placed in here may have on the Data Grid behaviour if you alter the UI or manipulate the visual collection. You can give it a try.

    The simpler method may be the best!


    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.


    • Edited by Xpert360 Saturday, March 16, 2013 10:08 AM
    • Marked as answer by JoshWilliam Monday, March 18, 2013 6:39 PM
    • Unmarked as answer by JoshWilliam Monday, March 18, 2013 6:40 PM
    Saturday, March 16, 2013 10:07 AM
  • Josh

    I'm glad that my post helped and I feel that it answered your original question. Seeing as you have re-asked your next question elsewhere (New Line Defaults from Parent in DataGrid) would you mark this one as answered please?
    I would also recommend @Yann's answer for your question by following his answer on Propagate Values from Master to Detail records.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Monday, March 18, 2013 5:14 PM