Articles in this section
Category / Section

How to Disable Edit Mode for Cells and Columns in WPF DataGrid?

3 mins read

In WPF DataGrid, you can disable editing for particular columns by setting the AllowEditing property to False. Also you can disable editing for particular cells by handling the CurrentCellBeginEdit event.

In the CurrentCellBeginEdit event, you can cancel the Edit mode for certain cells by setting the Cancel property to True. In the following code, the Edit mode is disabled for the cells if the value is null.

C#

            this.datagrid.CurrentCellBeginEdit += datagrid_CurrentCellBeginEdit;
private void datagrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs args)
    {
        var recordindex = this.datagrid.ResolveToRecordIndex
                                                                    (args.RowColumnIndex.RowIndex);
        if (recordindex >= 0)
        {
            var reflector = this.datagrid.View.GetPropertyAccessProvider();
            var recorddata = this.datagrid.View.GroupDescriptions.Count > 0 ?         this.datagrid.View.Records[recordindex] :
          this.datagrid.View.TopLevelGroup.DisplayElements[recordindex] as RecordEntry;
 
            var value = reflector.GetValue(recorddata.Data, args.Column.MappingName);
            if (value == null)
                args.Cancel = true;
        }
    }

The following example has disabled Edit mode for cells using the CurrentCellBeginEdit event. For differentiating the Edit mode disabled cells, you can set separate background color for those cells by customizing styles for GridCell. Here, you can change the background property of the GridCell based on value, using CellBackgroundConverter

Refer the following code snippet to apply GridCell Style.

XAML

<Window.Resources>
        <local:CellBackgroundConverter x:Key="cellbackgroundconverter"/>
</Window.Resources>
<syncfusion:GridTextColumn MappingName="EmployeeDesignation" >
                <syncfusion:GridTextColumn.CellStyle>
                    <Style TargetType="syncfusion:GridCell">
                        <Setter Property="Background" 
                                     Value="{Binding EmployeeDesignation, 
                              Converter = {StaticResource cellbackgroundconverter}}" />
                    </Style>
                </syncfusion:GridTextColumn.CellStyle>
</syncfusion:GridTextColumn>

Here, if the value of the cell is null, it means you have applied the background color Gray. The following code snippet for CellBackgroundConverter.

C#

public  class CellBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
        {
            if ( value==null)
                    return Brushes.Gray;
            return DependencyProperty.UnsetValue;
        }   
        
         public object ConvertBack(object value, Type targetType, 
         object parameter,    System.Globalization.CultureInfo culture)
       {
  throw new NotImplementedException();
       }

The following screenshot displays the difference between the Normal and Edit mode disabled cells.

Normal and Edit mode disabled cells

 

You can refer to the following samples to display the edit mode for cells in SfDataGrid with different background for the disabled cells.

WPF: DisableEditmode_WPF

WRT: DisableEditmode_Winrt


Conclusion

I hope you enjoyed learning about how to disable edit mode for cells and columns in WPF DataGrid.

You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Grid example 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