none
Lightswitch Grid Row ReOrder

    Question

  • Hi Everybody

    Is there any way to implement LightSwitch Grid Row Reorder for Silverlight Client?

    Any help would be appreciated...

    Thanks


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Monday, September 23, 2013 1:12 PM

Answers

  • This code deals with a table and grid of Relationships (between external people and staff members) and allows the relationships to be manually ordered by setting the RelationshipLevel property.

    Private Sub MoveUp_Execute() Dispatchers.Main.BeginInvoke(Sub() Dim objThis As Relationship = mobjRelationshipsGrid.SelectedItem Dim intLevel As Integer = objThis.RelationshipLevel Dim objPrior As Relationship = Relationships.Where(Function(r) r.RelationshipLevel = intLevel - 1).FirstOrDefault If objPrior IsNot Nothing Then objPrior.RelationshipLevel += 1 objThis.RelationshipLevel -= 1 SortAndSelect(objThis) End If End Sub) End Sub Private Sub MoveDown_Execute() Dispatchers.Main.BeginInvoke(Sub() Dim objThis As Relationship = mobjRelationshipsGrid.SelectedItem Dim intLevel As Integer = objThis.RelationshipLevel Dim objNext As Relationship = Relationships.Where(Function(r) r.RelationshipLevel = intLevel + 1).FirstOrDefault If objNext IsNot Nothing Then objNext.RelationshipLevel -= 1 objThis.RelationshipLevel += 1 SortAndSelect(objThis) End If End Sub) End Sub Private Sub SortAndSelect(ByVal objThis As Relationship) SortRelationshipsGrid() mobjRelationshipsGrid.SelectedItem = objThis Me.Details.Dispatcher.BeginInvoke(Sub() Relationships.SelectedItem = objThis End Sub) End Sub Private WithEvents mobjRelationshipsGrid As DataGrid Private Sub RelationshipGrid_ControlAvailable(sender As Object, e As ControlAvailableEventArgs) mobjRelationshipsGrid = DirectCast(e.Control, DataGrid) SortRelationshipsGrid() End Sub Private Sub SortRelationshipsGrid() Dispatchers.Main.BeginInvoke(Sub() Dim objView As New PagedCollectionView(mobjRelationshipsGrid.ItemsSource) Dim objCurrent As Object = objView.CurrentItem Using objView.DeferRefresh objView.SortDescriptions.Clear() objView.SortDescriptions.Add(New SortDescription("RelationshipLevel", ListSortDirection.Ascending)) End Using objView.MoveCurrentTo(objCurrent) mobjRelationshipsGrid.ItemsSource = objView End Sub) End Sub Private Sub mobjRelationshipsGrid_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles mobjRelationshipsGrid.SelectionChanged Dim objSelecteditem As Relationship If e.AddedItems Is Nothing OrElse e.AddedItems.Count = 0 Then objSelecteditem = Nothing Else objSelecteditem = e.AddedItems(0) End If FindControl("MoveDown").IsEnabled = objSelecteditem IsNot Nothing AndAlso objSelecteditem.RelationshipLevel < Relationships.Max(Function(r) r.RelationshipLevel) FindControl("MoveUp").IsEnabled = objSelecteditem IsNot Nothing AndAlso objSelecteditem.RelationshipLevel > 1

    End Sub



    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Tuesday, September 24, 2013 7:19 AM
  • Hi Rashmi,

    Here is code taken from a sample:

            private IContentItemProxy _grid;
            private DataGrid _datagrid;
            private bool _productsLoaded = false;
    
            partial void EditablePeriodsGrid_Created()
            {
                _grid = this.FindControl("grid");
                _grid.ControlAvailable += _grid_ControlAvailable;
            }
    
            partial void Periods_Loaded(bool succeeded)
            {
                _productsLoaded = true;
                if (_datagrid != null)
                    GroupGridColumns();
            }
    
            void _grid_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                _grid.ControlAvailable -= _grid_ControlAvailable;
                _datagrid = (DataGrid)e.Control;
                _datagrid.AlternatingRowBackground = new SolidColorBrush(Color.FromArgb(64,255,223,160));
                if (_productsLoaded)
                    GroupGridColumns();
            }
    
            void GroupGridColumns()
            {
                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                {
                    PagedCollectionView view = new PagedCollectionView(_datagrid.ItemsSource);
                    using (view.DeferRefresh())
                    {
                        // What do you want to group by
                        view.GroupDescriptions.Add(new PropertyGroupDescription("PeriodYear"));
    
                        // What do you want to order by
                        view.SortDescriptions.Add(new SortDescription("PeriodYear", ListSortDirection.Descending));
                        view.SortDescriptions.Add(new SortDescription("PeriodType", ListSortDirection.Ascending));
                        view.SortDescriptions.Add(new SortDescription("PeriodStart", ListSortDirection.Ascending));
                    }
                    view.MoveCurrentToFirst();
                    _datagrid.ItemsSource = view;
                });
            }

    This does grouping and sorting. You just need the sorting.

    It was taken from this gallery sample: Colors and more...

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.


    Monday, September 23, 2013 4:06 PM
  • The SelectionChanged event is used to control the Enablement of the Up/Down buttons so the Up button is disabled when the Level (of the selected item) is 1 and the Down button disabled when the Level is the maximum number. This stops the user pressing the buttons too many times, trying to move the row above the first place or below the last.

    The variable mobjRelationshipsGrid is declared WithEvents so it calls its SelectionChanged event automatically. That method should be placed alongside the rest of the code.

    I did it this way because using a PagedCollectionView to replace the Grid's ItemsSource every time seems to interfere with the standard Button_CanExecute events. You might try setting the underlying collection's SelectedItem property to see if that gives you a better result.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Tuesday, September 24, 2013 8:09 AM

All replies

  • You can override the sort order on the client by using a CollectionView and defining the sort columns. Look at it as the 'grid group by' solution without the grouping :)

    It is only usable with smaller data sets and no paging.

    If you need more information then just ask...

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Monday, September 23, 2013 1:40 PM
  • Hi Dave

    Thank you for your reply.

    If possible could you please provide some piece of code, so that it will be easy to understand.

    I am not using paging and there will be maximum 15-20 row in the grid. So obviously It will be small set of data.

    Thanks


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Monday, September 23, 2013 3:58 PM
  • Hi Rashmi,

    Here is code taken from a sample:

            private IContentItemProxy _grid;
            private DataGrid _datagrid;
            private bool _productsLoaded = false;
    
            partial void EditablePeriodsGrid_Created()
            {
                _grid = this.FindControl("grid");
                _grid.ControlAvailable += _grid_ControlAvailable;
            }
    
            partial void Periods_Loaded(bool succeeded)
            {
                _productsLoaded = true;
                if (_datagrid != null)
                    GroupGridColumns();
            }
    
            void _grid_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                _grid.ControlAvailable -= _grid_ControlAvailable;
                _datagrid = (DataGrid)e.Control;
                _datagrid.AlternatingRowBackground = new SolidColorBrush(Color.FromArgb(64,255,223,160));
                if (_productsLoaded)
                    GroupGridColumns();
            }
    
            void GroupGridColumns()
            {
                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                {
                    PagedCollectionView view = new PagedCollectionView(_datagrid.ItemsSource);
                    using (view.DeferRefresh())
                    {
                        // What do you want to group by
                        view.GroupDescriptions.Add(new PropertyGroupDescription("PeriodYear"));
    
                        // What do you want to order by
                        view.SortDescriptions.Add(new SortDescription("PeriodYear", ListSortDirection.Descending));
                        view.SortDescriptions.Add(new SortDescription("PeriodType", ListSortDirection.Ascending));
                        view.SortDescriptions.Add(new SortDescription("PeriodStart", ListSortDirection.Ascending));
                    }
                    view.MoveCurrentToFirst();
                    _datagrid.ItemsSource = view;
                });
            }

    This does grouping and sorting. You just need the sorting.

    It was taken from this gallery sample: Colors and more...

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.


    Monday, September 23, 2013 4:06 PM
  • Are you looking to:

    1. programmatically control which columns are sorted or
    2. implement a method of moving rows up and down in the grid?

    Dave Baker's code does the first, not the second.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Monday, September 23, 2013 4:57 PM
  • Hi Simon,

    Yes you are right. :)


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Monday, September 23, 2013 5:00 PM
  • Hi Simon

    I am looking for number two.

    Thanks


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Monday, September 23, 2013 6:04 PM
  • Ok, I've got some code to do that. I'll look it out and post it here as soon as possible.

    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Monday, September 23, 2013 6:37 PM
  • Thank You Simon

    Waiting for your reply.


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Monday, September 23, 2013 7:02 PM
  • This code deals with a table and grid of Relationships (between external people and staff members) and allows the relationships to be manually ordered by setting the RelationshipLevel property.

    Private Sub MoveUp_Execute() Dispatchers.Main.BeginInvoke(Sub() Dim objThis As Relationship = mobjRelationshipsGrid.SelectedItem Dim intLevel As Integer = objThis.RelationshipLevel Dim objPrior As Relationship = Relationships.Where(Function(r) r.RelationshipLevel = intLevel - 1).FirstOrDefault If objPrior IsNot Nothing Then objPrior.RelationshipLevel += 1 objThis.RelationshipLevel -= 1 SortAndSelect(objThis) End If End Sub) End Sub Private Sub MoveDown_Execute() Dispatchers.Main.BeginInvoke(Sub() Dim objThis As Relationship = mobjRelationshipsGrid.SelectedItem Dim intLevel As Integer = objThis.RelationshipLevel Dim objNext As Relationship = Relationships.Where(Function(r) r.RelationshipLevel = intLevel + 1).FirstOrDefault If objNext IsNot Nothing Then objNext.RelationshipLevel -= 1 objThis.RelationshipLevel += 1 SortAndSelect(objThis) End If End Sub) End Sub Private Sub SortAndSelect(ByVal objThis As Relationship) SortRelationshipsGrid() mobjRelationshipsGrid.SelectedItem = objThis Me.Details.Dispatcher.BeginInvoke(Sub() Relationships.SelectedItem = objThis End Sub) End Sub Private WithEvents mobjRelationshipsGrid As DataGrid Private Sub RelationshipGrid_ControlAvailable(sender As Object, e As ControlAvailableEventArgs) mobjRelationshipsGrid = DirectCast(e.Control, DataGrid) SortRelationshipsGrid() End Sub Private Sub SortRelationshipsGrid() Dispatchers.Main.BeginInvoke(Sub() Dim objView As New PagedCollectionView(mobjRelationshipsGrid.ItemsSource) Dim objCurrent As Object = objView.CurrentItem Using objView.DeferRefresh objView.SortDescriptions.Clear() objView.SortDescriptions.Add(New SortDescription("RelationshipLevel", ListSortDirection.Ascending)) End Using objView.MoveCurrentTo(objCurrent) mobjRelationshipsGrid.ItemsSource = objView End Sub) End Sub Private Sub mobjRelationshipsGrid_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles mobjRelationshipsGrid.SelectionChanged Dim objSelecteditem As Relationship If e.AddedItems Is Nothing OrElse e.AddedItems.Count = 0 Then objSelecteditem = Nothing Else objSelecteditem = e.AddedItems(0) End If FindControl("MoveDown").IsEnabled = objSelecteditem IsNot Nothing AndAlso objSelecteditem.RelationshipLevel < Relationships.Max(Function(r) r.RelationshipLevel) FindControl("MoveUp").IsEnabled = objSelecteditem IsNot Nothing AndAlso objSelecteditem.RelationshipLevel > 1

    End Sub



    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Tuesday, September 24, 2013 7:19 AM
  • Hi Simon

    Thank you for the code snippet. This is exactly what I am looking for.

    But I could not able to understand the role of SelectionChanged event. And where to implement SelectionChanged event.

    Thanks


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Tuesday, September 24, 2013 7:59 AM
  • The SelectionChanged event is used to control the Enablement of the Up/Down buttons so the Up button is disabled when the Level (of the selected item) is 1 and the Down button disabled when the Level is the maximum number. This stops the user pressing the buttons too many times, trying to move the row above the first place or below the last.

    The variable mobjRelationshipsGrid is declared WithEvents so it calls its SelectionChanged event automatically. That method should be placed alongside the rest of the code.

    I did it this way because using a PagedCollectionView to replace the Grid's ItemsSource every time seems to interfere with the standard Button_CanExecute events. You might try setting the underlying collection's SelectedItem property to see if that gives you a better result.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Tuesday, September 24, 2013 8:09 AM