Articles in this section
Category / Section

How to disable Edit mode for cells with different background for those disabled cells in WinRT DataGid?

2 mins read

In SfDataGrid, 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

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