Articles in this section
Category / Section

How to skip editing for read-only colums in AddNewRow of WPF DataGrid (SfDataGrid)?

1 min read

You can edit all the columns in AddNewRow including Read-Only columns as default in WPF DataGrid (SfDataGrid). But you can skip the editing for Read-Only columns by using CurrentCellBeginEdit event.

C#

this.grid.CurrentCellBeginEdit += grid_CurrentCellBeginEdit;
 
void grid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs args)
{
    var rowIndex = args.RowColumnIndex.RowIndex;
 
    //If the rowindex in AddNewRowIndex, we need to validate whether the property is readonly or not.
    if (this.grid.IsAddNewIndex(rowIndex))
    {
        //pdc - PropertyDescriptorCollection.
        var pdc = this.grid.View.GetItemProperties();
 
        //get the propertyinfo from PropertyDescriptorCollection.
        var propertyInfo = pdc.Find(args.Column.MappingName, true);
 
        //property is readonly you can cancel the editing of the cell.
        if (propertyInfo.IsReadOnly)
        {
            args.Cancel = true;
        }
    }
}

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 sign in to leave a comment
Access denied
Access denied