Articles in this section
Category / Section

How to update the table summary value when the cell in edit mode in UWP DataGrid (SfDataGrid) ?

1 min read

In SfDataGrid, you can update the summary values when you are changing the values by overriding OnInitializeEditElement method and SfNumericTextBox.ValueChanged event in GridNumericCelllRenderer.

dataGrid.AllowEditing = true;
dataGrid.LiveDataUpdateMode = LiveDataUpdateMode.AllowSummaryUpdate;
this.dataGrid.CellRenderers.Remove("Numeric");
this.dataGrid.CellRenderers.Add("Numeric", new CustomizedGridCellNumericRenderer(dataGrid));internal class CustomizedGridCellNumericRenderer : GridCellNumericRenderer
{
    RowColumnIndex RowColumnIndex;
    SfDataGrid DataGrid { get; set; }
    string newvalue = null;
 
    public CustomizedGridCellNumericRenderer(SfDataGrid dataGrid)
    {
        DataGrid = dataGrid;
    }
 
    public override void OnInitializeEditElement(DataColumnBase dataColumn, SfNumericTextBox uiElement, object dataContext)
    {
        base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
        uiElement.ValueChanged += UiElement_ValueChanged;
        this.RowColumnIndex.ColumnIndex = dataColumn.ColumnIndex;
        this.RowColumnIndex.RowIndex = dataColumn.RowIndex;
    }
 
    private void UiElement_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        newvalue = e.NewValue.ToString();
        UpdateSummaryValues(this.RowColumnIndex.RowIndex, this.RowColumnIndex.ColumnIndex);
    }
 
    private void UpdateSummaryValues(int rowIndex, int columnIndex)
    {
        string editEelementText = newvalue == "0" ? "0" : newvalue;
        columnIndex = this.DataGrid.ResolveToGridVisibleColumnIndex(columnIndex);
        if (columnIndex < 0)
            return;
        var mappingName = DataGrid.Columns[columnIndex].MappingName;
        var recordIndex = this.DataGrid.ResolveToRecordIndex(rowIndex);
        if (recordIndex < 0)
            return;
        if (DataGrid.View.TopLevelGroup != null)
        {
            var record = DataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
            if (!record.IsRecords)
                return;
            var data = (record as RecordEntry).Data;
            data.GetType().GetProperty(mappingName).SetValue(data, (int.Parse(editEelementText)));
        }
        else
        {
            var record1 = DataGrid.View.Records.GetItemAt(recordIndex);
            record1.GetType().GetProperty(mappingName).SetValue(record1, (int.Parse(editEelementText)));
        }
    }
}

Demo to show the summary values update when value changed

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied