Articles in this section
Category / Section

How to commit the edited values when binding Dictionary in SfDataGrid?

1 min read

By default, Dictionary does not notify property changes and hence the edited values will not get committed when bound with a Dictionary in SfDataGrid.

However, this can be achieved in SfDataGrid by customizing the cell renderers. Please follow the below steps.

  1. Customize the GridCellTextViewRenderer and override the CommitCellValue method.
  2. In the CommitCellValue method, obtain the edited value using the GetControlValue method. Then replace the actual value of the bound property with the edited value for the editing cell and update the new values in the grid using UpdateCellValue method.
  3. For refreshing the new cell value in view, call the RefreshDisplayValue method.
  4. Finally, you must modify the cell renderer mapping by replacing the existing TextView entry with the customized cell renderer in the SfDataGrid.CellRenderers collection.

Refer the below code example to commit the edited values in dynamic collection.

MainPage.cs

public class MainPage:ContentPage
    {        
        public MainPage ()
        {
            var grid = new SfDataGrid();
            grid.CellRenderers.Remove("TextView");
            grid.CellRenderers.Add("TextView", new GridCellTextViewRendererExt());
 
            grid.AllowEditing = true;
            grid.AutoGenerateColumns = false;
            grid.ColumnSizer = ColumnSizer.Star;
            grid.Columns.Add(new GridTextColumn()
            {
                HeaderText = "Company",
                MappingName = "Values[company]",
                LineBreakMode = LineBreakMode.WordWrap,
                TextAlignment = TextAlignment.Start,
                HeaderTextAlignment = TextAlignment.Start,
             });
            grid.Columns.Add(new GridTextColumn()
            {
                HeaderText = "Opentimes",
                MappingName = "Values[opentimes]",
                LineBreakMode = LineBreakMode.WordWrap,
                TextAlignment = TextAlignment.Start,
                HeaderTextAlignment = TextAlignment.Start,
                 });
             this.Content=grid;
      }
}

 

GridCellTextViewRendererExt.cs

public class GridCellTextViewRendererExt : GridCellTextViewRenderer 
{ 
    public GridCellTextViewRendererExt() 
    { 
 
    } 
 
    public async override void CommitCellValue(bool isNewValue) 
    { 
        var editedValue = this.GetControlValue(); 
        var editingColumn = this.DataGrid.Columns[this.CurrentCellIndex.ColumnIndex]; 
        var editingColumnName = editingColumn.MappingName.Substring(7, editingColumn.MappingName.Length - 8); 
        var dataColumn = (this.CurrentCellElement as GridCell).DataColumn; 
        (dataColumn.RowData as DataObject).Values[editingColumnName] = editedValue; 
        await Task.Delay(1); 
        this.UpdateCellValue(dataColumn); 
        this.RefreshDisplayValue(dataColumn); 
    } 
 
    protected override void OnUpdateCellValue(DataColumnBase dataColumn) 
    { 
        var cellValue = SfDataGridHelpers.GetCellValue(this.DataGrid, dataColumn.RowData, dataColumn.GridColumn.DisplayBinding.Path); 
        base.OnUpdateCellValue(dataColumn); 
    } 
} 

 

Screenshot:

C:\Users\pavithra.sivakumar\AppData\Local\Microsoft\Windows\INetCache\Content.Word\Editing cell.png

 

Sample Link: How to commit the edited values when binding Dictionary in SfDataGrid?

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