Skip to content

SyncfusionExamples/how-to-update-the-table-summary-value-when-the-cell-in-edit-mode-in-winforms-data-grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to Update the Table Summary Value When the Cell in Edit Mode in WinForms DataGrid?

This sample illustrates how to update the table summary value when the cell in edit mode in WinForms DataGrid (SfDataGrid).

In DataGrid, you can update the summary values when you are changing the value by overriding OnInitializeEditElement method and UiElement.TextChanged event in GridNumericCellRenderer.

C#

this.sfDataGrid.CellRenderers.Remove("Numeric");
this.sfDataGrid.CellRenderers.Add("Numeric", new GridNumericCellRendererExt(this.sfDataGrid));

public class GridNumericCellRendererExt : GridNumericCellRenderer
{
    RowColumnIndex RowColumnIndex { get; set; }

    SfDataGrid DataGrid { get; set; }

    public GridNumericCellRendererExt(SfDataGrid dataGrid)
    {
        this.DataGrid = dataGrid;
    }

    protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, SfNumericTextBox uiElement)
    {
        base.OnInitializeEditElement(column, rowColumnIndex, uiElement);
        uiElement.TextChanged += UiElement_TextChanged;
        this.RowColumnIndex = rowColumnIndex;
    }

    private void UiElement_TextChanged(object sender, EventArgs e)
    {
        UpdateSummaryValues(this.RowColumnIndex.RowIndex, this.RowColumnIndex.ColumnIndex);
    }

    private void UpdateSummaryValues(int rowIndex, int columnIndex)
    {
        string editEelementText = string.IsNullOrEmpty(this.CurrentCellRendererElement.Text) ? "0" : this.CurrentCellRendererElement.Text;
        columnIndex = this.TableControl.ResolveToGridVisibleColumnIndex(columnIndex);

        if (columnIndex < 0)
            return;

        var mappingName = DataGrid.Columns[columnIndex].MappingName;
        var recordIndex = this.TableControl.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)));
        }
    }
}

DataGrid displays the updated table summary upon cell editing

Requirements to run the demo

Visual Studio 2015 and above versions

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages