ItemAdding
This rule is triggered when a new record is created, before it is added at the end of the data source.
This can be done :
- by a user clicking on an
Addbutton. - by code when calling
AddNewAsync,AddAsync,InsertNewAsyncorInsertAsync
It can be used, for example, to initialize the values of the newly created item.
Warning
When the rule is triggered, the item is not yet added to the view model datasource. Thus, modifying the value of its properties inside an ItemAdding rule does not trigger the associated PropertyChanged rules.
Warning
Adding an item does not persist it. The item is locally added to the datasource but is not sent to the server until saving.
See this article for more information on event rules execution order.
Arguments
The Arguments property provides the following options.
| Name | Type | Description |
|---|---|---|
Item |
The type of the UI view model | Instance of the item being added to the datasource. |
Cancel |
bool |
If set to true, the item will not be added to the datasource. |
MarkAsVirtual |
bool |
If set to true, the item will be mark as virtual once the item has been added. |
The MarkAsVirtual option is true when the item is being added via the add row of the datagrid, otherwise it is false.
Item versus DatasourceCurrent
Item refers to the item that has been added. It is the same as using Arguments.Item.
Important
In an ItemAdding event rule, since the item is not yet added to the datasource, DatasourceCurrent is always different from Item.
To know more on the difference between the Item and DatasourceCurrent properties in an UI view event rule, you can check this article.
Examples
Example 1
Initializing the OrderDate property with the current date.
Item.OrderDate = DateTime.Today;
Example 2
Initializing the OrderDate property with the date of the previously selected record.
if (DatasourceCurrent == null)
{
// The datasource has no records, we initialize the date to the current date
Item.OrderDate = DateTime.Today;
}
else
{
// Initializing the date with the date of the previously selected record
Item.OrderDate = DatasourceCurrent.OrderDate;
}