Unsorted Datagrid items move while editing

Posted by: abellisomi on 22 November 2018, 9:36 am EST

    • Post Options:
    • Link

    Posted 22 November 2018, 9:36 am EST

    All the datagrids in my application have this strange behavior. When none of their columns are sorted and rows are edited, on lose focus those are “reshuffled” around creating a sub-optimal user experience. E.g:

    1 A

    2 B

    3 C

    If I edit A it gets resorted

    2 B

    3 C

    1 A

    Then if I edit C

    2 B

    1 A

    3 C

    Is this a normal/intended behavior?

    Thanks,

  • Posted 25 November 2018, 9:13 pm EST

    Hello,

    We could replicate the above said behavior only when grouping has been applied in C1DataGrid. So, just to be sure that we are in right direction, can you please clarify whether you are doing the same or not?

    Also, If this is not the scenario then a detailed explanation of the scenario in which the behavior is being occurring would be helpful.

    Thanks and Regards,

    Basant

  • Posted 9 December 2018, 11:39 pm EST

    Basant,

    yes, that happens on grouping

    Thanks

  • Posted 10 December 2018, 5:03 pm EST

    Hi,

    Thanks for clarification. By default C1DataGrid applies sorting on the column that you grouped by drag-drop to Group panel. And since inside that group all items will be considered equal for sorting, so a decision for sorted position is made on the basis of FCFS (i.e first item to be decide on will be the first one in sorting order). So, It seems like when a row is edited the item bound to it will be re-considered for sorted position and hence the last one in that group (i.e. observed behavior).

    As per your specifications it seems like you don’t want sorting to be applied, so as a workaround you can disable default sorting due to grouping as follows:

    
    private void dataGrid_GroupChanged(object sender, DataGridGroupChangedEventArgs e)
    {
     foreach (var groupDesc in e.NewGroupedColumns)
     {
      if (e.OldGroupedColumns.Where(x => x.Column == groupDesc.Column && x.Value != groupDesc.Value).Count() > 0)
       break;
      dataGrid.GroupBy(groupDesc.Column, DataGridSortDirection.None, true);
     }
    }
    
    

    Also, you can refer the attached sample (prj_DoNotMoveRows.zip) for the same.

    Thanks and Regards,

    Basant

    prj_DoNotMoveRows.zip

  • Posted 15 May 2019, 1:53 am EST

    Basant,

    thanks a lot for the example you provided and please accept my apologies for taking so long to get back to you.

    Out of the box the solution you provide works however, unfortunately, the issue hasn’t been resolved.

    To reproduce it, I added a new column to the Product object

    new Product{OneMore ="X", Name="A", Line ="A1", Price=10, Discontinued = false},
    

    So to have the product list having the same value for every items

    _products = new List<Product>()
                {
                    new Product{OneMore ="X", Name="A", Line ="A1", Price=10, Discontinued = false},
                    new Product{OneMore ="X", Name="A", Line ="A2", Price=15, Discontinued = true},
                    new Product{OneMore ="X", Name="A", Line ="A3", Price=20, Discontinued = false},
                    new Product{OneMore ="X", Name="B", Line ="B1", Price=10, Discontinued = false},
                    new Product{OneMore ="X", Name="B", Line ="B2", Price=15, Discontinued = true},
                    new Product{OneMore ="X", Name="B", Line ="B3", Price=20, Discontinued = false},
                    new Product{OneMore ="X", Name="C", Line ="C1", Price=10, Discontinued = false},
                    new Product{OneMore ="X", Name="C", Line ="C2", Price=15, Discontinued = true},
                    new Product{OneMore ="X", Name="C", Line ="C3", Price=20, Discontinued = false},
    
                };
    

    Now, the steps to replicate the issue are:

    1. sort the column by OneMore
    2. group by Name

    Now the grid is sorted by OneMore and the shuffling of the rows on editing still happens.

    Thanks for the attention

  • Posted 15 May 2019, 1:56 am EST

  • Posted 15 May 2019, 9:46 pm EST

    Hi,

    I also specified earlier that the issue occurs when sorting is applied. Actually, DataGrid internally uses MS CollectionView which removes and then re-insert the edited item to re-group/sort it and hence the behavior. So, we are sorry to inform you that the behavior is by design.

    However, DataGrid provides an EndEditRow() method which can be used to deny commit changes so that re-group/sort is not performed by CollectionView. So, we can use this to resolve the issue only for the case when an item is unchanged before and after editing. Please refer the attached sample for the same (prj_DontMoveUnchangedRow.zip).

    Thanks and Regards,

    Basant

    prj_DontMoveUnchangedRow.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels