Articles in this section
Category / Section

How to update the data values while editing mode in WPF DataGrid (SfDataGrid)?

1 min read

WPF DataGrid (SfDataGrid) does not provide the direct support to update the data values while editing. You can update the values while editing by overriding OnInitializeEditElement method and customize the TextBox.TextChanged event in GridCellTextBoxRenderer in WPF DataGrid (SfDataGrid).

this.dataGrid.CellRenderers.Remove("TextBox");
this.dataGrid.CellRenderers.Add("TextBox", new GridCellTextBoxRendererExt(dataGrid));
 
public class GridCellTextBoxRendererExt : GridCellTextBoxRenderer
{
        RowColumnIndex RowColumnIndex;
        SfDataGrid DataGrid { get; set; }
        string newvalue = null;
 
        public GridCellTextBoxRendererExt(SfDataGrid dataGrid)
        {
            DataGrid = dataGrid;
        }
        
        public override void OnInitializeEditElement(DataColumnBase dataColumn, TextBox uiElement, object dataContext)
        {
            base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
          
            uiElement.TextChanged += UiElement_TextChanged;
            this.RowColumnIndex.ColumnIndex = dataColumn.ColumnIndex;
            this.RowColumnIndex.RowIndex = dataColumn.RowIndex;
        }           
 
        private void UiElement_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textbox = (e.OriginalSource as TextBox);
            if (textbox.IsFocused)
            {
                newvalue = (e.OriginalSource as TextBox).Text;
                UpdateValues(this.RowColumnIndex.RowIndex, this.RowColumnIndex.ColumnIndex);
            }
        }     
 
        private void UpdateValues(int rowIndex, int columnIndex)
        {
            string editEelementText = 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, editEelementText);
            }
            else
            {
                var record1 = DataGrid.View.Records.GetItemAt(recordIndex);
                record1.GetType().GetProperty(mappingName).SetValue(record1, editEelementText);
            }
        }
}

WPF DataGrid (SfDataGrid) provides support for various built-in column types. Each column has its own properties and renderer for more details please refer the below documentation link.

Documentation Link: https://help.syncfusion.com/wpf/datagrid/column-types

Shows the update the data values while editing in SfDataGridTake a moment to peruse the WPF DataGrid - Editing documentation, where you can find about editing with code examples.

You can download the example from GitHub.


Conclusion

I hope you enjoyed learning about how to update the data values in WPF DataGrid .

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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