Search code examples
datagridsyncfusionwinui-3

DataGrid gets the value of a row and column


I want to get the value of a certain cell in the DataGrid of syncfusion.

like this:

<dataGrid:SfDataGrid x:Name="sfDataGrid">         
</dataGrid:SfDataGrid>

C#:

           Sfdatagrid\[i,j\] is useless.

How do I get the value of this cell.

https://help.syncfusion.com/winui/datagrid/overview The document In this official document, I didn't find out how to get the value of this cell according to the index of rows and columns.

I want to get the value of this cell according to the index of the row and column.

Column A Column B
Cell 1 Cell 2
Cell 3 Cell 4

For example, I want to get the value of this cell according to the index of the row and column.

How should I write code in c#


Solution

  • Your requirement to get the value of this cell according to the index of the row and column in SfDataGrid can be achieved by using View.GetPropertyAccessProvider.GetValue method in SfDatGrid. Please refer the below code snippet,

       private void OnGetCellValueClicked(object sender, RoutedEventArgs e)
       {
            // Get the cell value for RowIndex = 5 and ColumnIndex = 2            
            int rowIndex = 5;
            int columnIndex = dataGrid.ResolveToGridVisibleColumnIndex(2);
    
            object record = null;
    
            if (columnIndex < 0)
                return;
    
            var provider = this.dataGrid.View.GetPropertyAccessProvider();           
    
            var mappingName = dataGrid.Columns[columnIndex].MappingName;
    
            var recordIndex = this.dataGrid.ResolveToRecordIndex(rowIndex);
    
            if (this.dataGrid.View.TopLevelGroup != null)
            {
                var displayElement = this.dataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
    
                if (displayElement == null)
                    return;
                if (displayElement is RecordEntry)
                    record = ((RecordEntry)displayElement).Data;
            }
            else
            {
                record = this.dataGrid.View.Records[recordIndex].Data;
                if (record == null)
                    return;
            }            
            var cellValue = provider.GetValue(record, mappingName);
    
            //here display the cell value for RowIndex = 5 and ColumnIndex = 2
            if (cellValue != null)
                txtGetCellValue.Text = cellValue.ToString();
       }