How to get sfDataGrid current seleted row index?

Hi

sfDataGrid is selection mode is Cell only,

I get the selected cells by the following code.

try to get by RowIndex, but it returns -1 no matter what row it is.

            foreach(GridCellInfo gci in SP_DataTable.GetSelectedCells())
            {
                 MessageBox.Show(gci.RowIndex.ToString());
            }

1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team December 7, 2021 01:56 PM UTC

Hi Johnes,

Thank you for contacting Syncfusion Support.

Solution 1:

Your requirement to get the
selected cells row index in SfDataGrid can be achieved by passing the RowData into ResolveToRowIndex method in SfDataGrid. Please refer below code snippet,

 
private void OnGetSelectedcellsRowIndexClicked(object sender, RoutedEventArgs e) 
{ 
            foreach (GridCellInfo gci in sfDataGrid.GetSelectedCells()) 
            { 
                //get the RowIndex by passing RowData in SfDataGrid                 
                var getRowIndex = sfDataGrid.ResolveToRowIndex(gci.RowData); 
                 
                MessageBox.Show("Get Selected Cells RowIndex :" + getRowIndex.ToString());                
            } 
} 

UG Link: https://help.syncfusion.com/wpf/datagrid/helpers#example-you-can-find-the-record-index-from-row-index-using-resolvetorecordindex-method

Solution 2:

Based on provided information we suspect that your requirement to get the
currently selected item row index in SfDataGrid can be achieved by using SfDataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex property. Please refer below code snippet,

 
private void OnGetSelectedcellsRowIndexClicked(object sender, RoutedEventArgs e) 
{ 
            //Get the Current Row index from the CurrentCellManager 
            var currentRowIndex = sfDataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex; 
 
             MessageBox.Show(" CurrentIndex :" + currentRowIndex.ToString());                
 
} 
 
Regards, 
Vijayarasan S

Marked as answer
Loader.
Up arrow icon