Get SelectedCells order from top left to bottom right

Hi @all!

I have a SfDataGrid with SelectionUnit set to Any and SelectionMode set to Multiple. Now the user can select some cells and can tap on a button and i will handle the selected cells.
But when i call GetSelectedCells on the SfDataGrid, i will get the cells in the order how they was selected, but i need to get the selected cells from the first cell to the last cell.

For example: user selects the cell B3 first, dann B2, then B1, then A3 and lastly A2. No when i call the GetSelectedCells function, i will get the Items B3, B2, B1, A3 And A2.
But i need to get the items like: A2, A3, B1, B2 and B3. (A2 and A3 are the first row in the grid, and B1/2/3 are the second row in the grid).

How can this be achieved?

Thanks for any help
Greetings

Thomas

3 Replies

JG Jai Ganesh S Syncfusion Team February 9, 2017 04:50 PM UTC

Hi Thomas, 
 
You can achieve your requirement to get the selected cells always based on the minimum row index by using the below code, 
 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    List<int> rowIndex = new List<int>(); 
    List<GridCellInfo> listOfCells = new List<GridCellInfo>(); 
    var cells = this.sfGrid.GetSelectedCells(); 
 
    //Get the row index based on the selected cells and sorted it 
    foreach(var item in cells) 
    { 
        var rindex = this.sfGrid.ResolveToRowIndex((item as GridCellInfo).RowData); 
        rowIndex.Add(rindex); 
    } 
    rowIndex.Sort(); 
 
    //Get the Selected cells based on the sorted row index  
    for (int j=0;j<rowIndex.Count;j++) 
    { 
        foreach (var item in cells) 
        { 
            var rindex = this.sfGrid.ResolveToRowIndex((item as GridCellInfo).RowData); 
 
        if (rindex == rowIndex[j]) 
            listOfCells.Add(item); 
        } 
 
    } 
} 
 
 
Regards, 
Jai Ganesh S 
 



TK Thomas Klocker February 10, 2017 12:59 PM UTC

Hi Jai,

very much thanks. works great!

Greetings
Thomas



JG Jai Ganesh S Syncfusion Team February 11, 2017 04:06 AM UTC

Hi Thomas, 
Thank you for the update. 
Please let us know if you need further assistance on this. 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon