C1DataGrid, sort and move DataRow

Posted by: inexcon on 7 August 2017, 7:42 am EST

  • Posted 7 August 2017, 7:42 am EST

    Hi!


    I have a C1DataGrid that the source is DataTable.DefaultView (so DataView).  When the C1DataGrid is sorting, it sorts the DataView, and the DataTable is not modified. Then I would like to move a row in the C1DataGrid.  Can I move rows directly from the DataView ?   


    Let's say if I don't sort, DataView and DataTable are identical. So if move a row from DataTable, the row is moved on the C1DataGrid. However, I cannot do that when the DataView is sorted because the position of rows in DataView and DataTable does not match.


    Do you have a sample to move a row by code after data is sorted in a grid using a DataTable/DataView?

  • Replied 7 August 2017, 7:42 am EST

    I am not sure I fully understand the scenario.


    All the state in the C1DataGrid is delegated to the ICollectionView which is in its ItemsSource. In your case, that's the DataView.


    Any CollectionView is just a view for  a given collection, which support for several features like Sorting, filtering, etc. If your view is sorted, then the data in the view takes sorting into account. You cannot mix the sorting with custom sorting, because in such scenario the dataview would be in an inconsistent state (it would say it is sorted by "ProductName" but it isn't actually true).


    What you can do to support your scenario, is to make the regular sorting work for you instead. The way to do that is adding for example a new column to the DataTable (say: MyCustomIndex) and then, sort the CollectionView for that column (or for that column and ProductName or whatever)


    Please let me know if that helps.


    Regards

  • Replied 7 August 2017, 7:42 am EST

    Hello


    There is my scenario:


    I want to move Row by code. Move row by code is working if I move them in the DataTable.   However, if the user can sort rows, then nothing work anymore.The rows displayed into the grid are not at the same position in the DataTable, so I cannot longer move them in the DataTable to make them move in the grid.


    How can I move rows by code after the user sort the grid?  For example, I have two button "Move Up" and "Move Down" so when the user presses the buttons I need to move up/down the selected row.

  • Replied 7 August 2017, 7:42 am EST

    I guess the scenario is not right defined.


    You either sort by hand or automatically by column values.  You cannot do both, that's inconsistent. What's the state of the CollectionView then?


    The way to model what you want would be to add a property to the DataTable ("CustomIndex"). When the user "moves" a row by hand, what you actually do is to update all the CustomIndex values based in the current view ordering (i.e. Sorted by "Age"), then you change the indexes of the rows you want to reflect the movement, and lastly you sort by that column.


    Then, the CollectionView will be sorted by CustomIndex, in a consistent state.


    Regards

  • Replied 7 August 2017, 7:42 am EST

    I'am saying that because in VB6, I was using C1FlexGrid and after a Sort I was able to move rows (without having to create a CustomIndex column), and I just want the same capabilities in Silverlight.

  • Replied 7 August 2017, 7:42 am EST

    But after I did a sort on CustomIndex, if the user click on Age again does the grid will sort Ascending or Descending ? Will the user be able to switch from Ascending/Descending, let's say if after a sort I re-sort by CustomIndex ?


    Good point. In order to accomplish that, you will need to add the ColumnIndex to the sort collection instead of replacing it. For example, in your sample, you would sort by ColumnIndex and Age (in that order) instead of just by ColumnIndex.


    But maybe you need more special handling. If the user clicks Age again (to change from ascending to descending), do you want to preserve the custom changes or do you want to sort by Age entirely? The default approach would be to remove the special changes and sort by Age.

    Other approach I can think of is to change all the SortMemberPath (not putting the same than the column itself). For example, for the Age column you might put CalculatedAge as SortMemberPath. Then, in the dataview you might have those columns with your custom logic..


    I'am saying that because in VB6, I was using C1FlexGrid and after a Sort I was able to move rows (without having to create a CustomIndex column), and I just want the same capabilities in Silverlight.


    Yes, I understand. There are two things here: our C1DataGrid follows the built-in DataGrid object model and philosophy. In particular, we delegate the full view state to the CollectionVIew and react to its changes. If we allow to make changes to the control which conflicts with the state in the CollectionView there is no way we can keep the information consistent. (i.e. we receive a CollectionChanged event with type Add in the index 10, what should we do? C1DataGrid doesn't know how to sort, it's made by the CollectionView. How do you do if the data comes from RIA services instead?).


    The correct way in Silverlight to do this, is to make your own "ModelView", in this case, implement your own ICollectionView. But that's a fairly complicated task, that's why I am suggesting these workarounds adding calculated columns and using the built-in sorting.


    One of the things we don't support in the C1DataGrid is an "unbound mode". Maybe the Silverlight C1FlexGrid can help you in this scenario in a better way as it does support unbound mode and has a similiar API to the one in WinForms, which allows you to do "less-silverlighty" things...


    Hope this helps!

  • Replied 7 August 2017, 7:42 am EST

    Yes, in my case, if the user sort again the custom changes are lost. So I will try your approach.


    Thank you for your help.

  • Replied 7 August 2017, 7:42 am EST

    OK So if I understand, I have let say two column, Age and CustomIndex (hidden).  After user click on Age, it does a Ascending Sort on Age. I update the CustomIndex from 1 to Rows. Then I sort by CustomIndex.  So now to move rows I need to change the value in the CustomIndex and sort again.


    But after I did a sort on CustomIndex, if the user click on Age again does the grid will sort Ascending or Descending ? Will the user be able to switch from Ascending/Descending, let's say if after a sort I re-sort by CustomIndex ?

  • Replied 12 December 2017, 8:56 pm EST

    Hello

    can you give me a sample code for that issue.
  • Marked as Answer

    Replied 26 December 2017, 6:02 am EST

    Hello,

    Since this thread discusses issues with different scenarios, could you please share what issue exactly are you running into?
    Having this information would help us have a clear understanding of the problem you are facing and in helping you effectively.

    Thanks,
    Ruchir Agarwal
Need extra support?

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

Learn More

Forum Channels