Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
270
How to determine the order of selected rows?
posted

Hi there,

I successfully implemented row drag & drop as described in the knowledge base. The only drawback I have is the order of the dragged rows. So when the user has selected the rows 3,6,9 and 12 I want to move the rows in exactly that order. But the SelectedRowsCollection does not save the order so I get inpredictable results when dragging the rows (like the new order is 9,6,12 and 3).

I tried to sort the selected rows using LINQ by row.Index but it does not seem to work because the row indices seem to change every time the user drags the selection.

Thank you for any help.

Achim

  • 469350
    Suggested Answer
    Offline posted

    The rows in the Selected.Rows collection will be in the order in which they were selected. But I imagine you probably want them in the order in which they appear in the grid.

    moojoo said:
    I tried to sort the selected rows using LINQ by row.Index but it does not seem to work because the row indices seem to change every time the user drags the selection.

    That's the hard way to do it. The easy way is to call:

    this.ultraGrid1.Selected.Rows.Sort();

     

  • 69832
    Suggested Answer
    Offline posted

    moojoo said:
    But the SelectedRowsCollection does not save the order so I get inpredictable results when dragging the rows (like the new order is 9,6,12 and 3).

    The SelectedRowsCollection does actually preserve the order in which the rows were selected. I looked at the KB article and it would seem that after the RowsCollection.Move method is called, the 'dropIndex' stack variable will no longer reference the same row (that it did before the first call to the Move method) because the act of moving the row can potentially change the indexes of the drop row and any other rows between that row and the ones being dropped. I'm not sure if this will work, but you might want to try updating that dropIndex with each call to the Move method, i.e., hold a reference to that drop row, and check its Index property with each iteration, rather than a stored value.