Disabled Cell Behavior
- 4 minutes to read
Overview
This Behavior allows you to disable cells in rows that meet a certain condition. Disabled cells are grayed-out according to the 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
.
Supported Controls
How to Attach the Behavior to a Control in the Designer
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.
Options
When the Behavior is attached, use the editor to specify the following options:
- The
Appearance
property — appearance settings applied to disabled cells. The default settings depend on the current skin. This property allows you to adjust them. For example, you can change the background color, font style, and so on. The
Expression
property — a string value that specifies a condition that disabled rows should meet. For example,DateDiffMonth([Sales Date], Today()) > 6
disables rows with orders that are older than six months.Click the property’s ellipsis button to invoke the expression editor.
The
ProcessingCell
event — allows you to enable/disable a cell.Use the
FieldName
event arguments to get the processed cell’s field name. TheRecordId
argument identifies the row/node/record that contains the processed cell. This argument returns the following values depending on the attached control:- For a GridControl — the index of the data source record that corresponds to the processed grid row. You can use the GetRowHandle(Int32) method to get the grid row’s handle. To get the processed data source record, use the GetRow(Int32) method.
- For a VGridControl — the index of the data source record that corresponds to the processed grid record. To get the processed data source record, use the GetRecordObject(Int32) method.
- For a TreeList — the processed node’s Id property value. To get the processed data source record, use the GetRow(Int32) method.
Set the
Disabled
argument to true to disable the processed cell. The code below disables rows only if the user checked a dedicated option (a standalone CheckEdit -checkEdit1
), but never disables cells in the ‘Order ID’ column.using DevExpress.Utils.Behaviors.Common; 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(); }
You can add an event handler in the editor’s events tab, or use the
DisabledCellEvents
component in the Properties window.
Important
Customizations made in the ShowingEditor or RowCellStyle event handlers take priority over Disabled Cell Behavior.
How to Attach the Behavior to a Control in Code
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);