TreeList.ValidateNode Event
Gives you the ability to specify whether a modified node’s data is valid, and if this node can lose focus.
Namespace: DevExpress.XtraTreeList
Assembly: DevExpress.XtraTreeList.v24.1.dll
NuGet Packages: DevExpress.Win.Navigation, DevExpress.Win.TreeList
Declaration
Event Data
The ValidateNode event's data class is ValidateNodeEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
ErrorText | Gets or sets the error description. |
Node | Gets the current Tree List node. Inherited from NodeEventArgs. |
Valid | Gets or sets whether the node’s validation succeeds. |
Remarks
The ValidateNode event is raised when a modified node is about to lose focus. Handle this event to specify whether node data is valid, and whether moving focus is allowed.
The inspected node is identified by the NodeEventArgs.Node parameter. To obtain the cell value, you can pass a column as an indexer to the node. After node values have been obtained, analyze them to determine whether the node’s data is valid. If not, set the ValidateNodeEventArgs.Valid parameter to false. This will result in displaying an error message whose text is specified by the ValidateNodeEventArgs.ErrorText parameter.
Clicking the Yes button returns focus to the node. This allows end-users to correct the values entered. If the No button is clicked, node focus is moved and the previously focused node’s data changes are discarded.
If node validation fails, you can indicate the cell containing an invalid value. Call the TreeList.SetColumnError method for this purpose. The method takes the column object and error description text as parameters. As a result, the cell within the specified column displays an error icon (). Pointing to the icon invokes the hint with the appropriate error description.
Note that the result of handling the ValidateNode event can be overridden by handling the TreeList.InvalidNodeException event.
Example
Assume that there are two columns in a Tree List: “OrderDate” and “RequiredDate”. The value in the first column must be less than the value in the second one. So we need to validate a node when it is about to be saved to the data source. For this purpose, the TreeList.ValidateNode
event is handled.
If a node fails validation, we set errors for the columns with corresponding descriptions using the TreeList.SetColumnError method. The descriptions will be displayed when hovering over the error icons.
The TreeList.InvalidNodeException event is handled in order to suppress displaying the default error message box.
The following screenshot shows the Tree List after a node fails validation.
Note that the TreeListOptionsBehavior.ShowToolTips option must be enabled to allow tooltips to be displayed.
using DevExpress.XtraTreeList;
private void treeList1_ValidateNode(object sender, ValidateNodeEventArgs e) {
DateTime time1 = (DateTime)e.Node[colOrderDate];
DateTime time2 = (DateTime)e.Node[colRequiredDate];
if(time1 >= time2) {
e.Valid = false;
//Set errors with specific descriptions for the columns
treeList1.SetColumnError(colOrderDate, "The value must be less than RequiredDate");
treeList1.SetColumnError(colRequiredDate, "The value must be greater than OrderDate");
}
}
private void treeList1_InvalidNodeException(object sender, InvalidNodeExceptionEventArgs e) {
//Suppress displaying the error message box
e.ExceptionMode = ExceptionMode.NoAction;
}