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
20
Selecting a row programmatically after sorting a column
posted

After sorting a column, when I try to select a row programmatically, the record number that I use to select a row is not the same any more. How do I get the right row number from the record number that I hold to select a row after a column is sorted?

  • 6759
    Offline posted

    Hello,

    I assume that by saying "record number" you mean the value of the row's Index property. It is expected for this index to change when doing sorting, filtering etc. since it indicates the index of the row in the current view.

    So what could you do is to store the underlying data object of the rows(row.Data - basically the object from the ItemsSource collection) you need to select. Then when you need them to get selected you could enumerate the Rows collection of the grid and find the rows representing the data items you stored on the on the first step and set their IsSelected property to true.

    The code for the second step should look something like this:

                List<object> dataObjectsForTheRowsToBeSelected= new List<object>(); // assuming you have stored here the data items of hte rows that need to be selected

                IEnumerable<Row> rowsToBeSelected = xamGrid1.Rows.Where(row => dataObjectsForTheRowsToBeSelected.Contains(row.Data));

                foreach (var row in rowsToBeSelected)
                {
                    row.IsSelected = true;
                }

    Please let me know if you need more assistance on the matter.