windowsforms-401146-common-features-behaviors-disabled-cell-behavior.md
The Disabled Cell Behavior allows you to disable cells in data rows that meet a certain condition. Disabled cells are grayed-out according to active skin settings. Users cannot edit disabled cells. For example, you can disable rows that display orders that are older than six months.
Note
The Disabled Cell Behavior does not prevent users from pasting (Ctrl+V) data into disabled Data Grid or Tree List cells, if the control’s OptionsClipboard.PasteMode property is set to Update.
To attach the Behavior to a control, use the standard approach:
Drop the BehaviorManager component from Visual Studio’s Toolbox onto the component tray.
Click Edit Behaviors in the component’s smart tag menu.
In the Add drop-down menu, select Disabled Cell Behavior to create the behavior.
Select the Disabled Cell Behavior. Use its Target property to attach the behavior to a control.
Appearance – Specifies appearance settings applied to disabled cells. You can change the background color, font style, etc. Default appearance settings depend on the active skin.
Expression – Specifies a condition that disabled rows should meet. For example, DateDiffMonth([SalesDate], Today()) > 6 disables rows with orders that are older than six months.
ProcessingCell – Handle this event to enable/disable cells based on a specific condition.
You can add an event handler in the Behavior editor’s events tab, or use the DisabledCellEvents component in the Properties window.
Important
Customizations made in ShowingEditor and/or RowCellStyle event handlers take priority over Disabled Cell Behavior.
The following example disables rows only if the user checked the check box displayed on the Form (the checkEdit1 control), but never disables cells in the ‘Order ID’ column.
using DevExpress.Utils.Behaviors.Common;
public Form1() {
InitializeComponent();
disabledCellEvents1.ProcessingCell += DisabledCellEvents1_ProcessingCell;
}
private void disabledCellEvents1_ProcessingCell(object sender, ProcessCellEventArgs e) {
bool evaluationResult = e.Disabled;
evaluationResult = checkEdit1.Checked ? evaluationResult : false;
e.Disabled = e.FieldName == "OrderID" ? false : evaluationResult;
}
// Use the grid view's LayoutChanged method to force the Behavior
// to recalculate disabled cells.
private void checkEdit1_CheckedChanged(object sender, EventArgs e) {
gridView1.LayoutChanged();
}
Imports DevExpress.Utils.Behaviors.Common
Public Sub New()
InitializeComponent()
disabledCellEvents1.ProcessingCell += DisabledCellEvents1_ProcessingCell
End Sub
Private Sub disabledCellEvents1_ProcessingCell(ByVal sender As Object, ByVal e As ProcessCellEventArgs) _
Handles disabledCellEvents1.ProcessingCell
Dim evaluationResult As Boolean = e.Disabled
evaluationResult = If(checkEdit1.Checked, evaluationResult, False)
e.Disabled = If(e.FieldName = "OrderID", False, evaluationResult)
End Sub
' Use the grid view's LayoutChanged method to force the Behavior
' to recalculate disabled cells.
Private Sub checkEdit1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles checkEdit1.CheckedChanged
gridView1.LayoutChanged()
End Sub
The following example demonstrates how to attach the Behavior to a Grid control in code, specify its settings, and add event handlers:
using DevExpress.Utils.Behaviors;
using DevExpress.Utils.Behaviors.Common;
BehaviorManager bm;
public MyForm() {
InitializeComponent();
bm = new BehaviorManager(this.components);
bm.Attach<DisabledCellBehavior>(gridView1, behavior => {
behavior.Properties.Expression = "DateDiffMonth([Sales Date], Today()) > 6";
behavior.Properties.Appearance.BackColor = System.Drawing.Color.AliceBlue;
behavior.Properties.Appearance.FontStyleDelta = System.Drawing.FontStyle.Italic;
behavior.ProcessingCell += (s, e) => { if (!checkEdit1.Checked) e.Disabled = false; };
});
}
// Use the Detach method to remove the Behavior.
bm.Detach<DisabledCellBehavior>(gridView1);
Imports DevExpress.Utils.Behaviors
Imports DevExpress.Utils.Behaviors.Common
Dim bm As BehaviorManager
Public Sub MyForm()
InitializeComponent()
bm = New BehaviorManager(Me.components)
bm.Attach(Of DisabledCellBehavior)(gridView1, Sub(behavior)
behavior.Properties.Expression = "DateDiffMonth([Sales Date], Today()) > 6"
behavior.Properties.Appearance.BackColor = System.Drawing.Color.AliceBlue
behavior.Properties.Appearance.FontStyleDelta = System.Drawing.FontStyle.Italic
AddHandler behavior.ProcessingCell, Sub(s, e)
If Not checkEdit1.Checked Then
e.Disabled = False
End If
End Sub
End Sub)
End Sub
' Use the Detach method to remove the Behavior.
bm.Detach(Of DisabledCellBehavior)(gridView1)