How to enable add to grid only one row for some conditions ?
Add to grid only one row
Best answer by Dipak Nilkanth
Hi
You can handle this in the graph code by controlling the detail view’s AllowInsert property and validating the condition in the detail DAC’s RowInserting event.
For example,
1. Disable the Add Row (+) button
In the parent DAC’s RowSelected event, check whether a detail record already exists.
protected virtual void _(Events.RowSelected<MyHeader> e)
{
if (e.Row == null)
return;
MyDetail row = Details.SelectWindowed(0, 1);
Details.Cache.AllowInsert = row == null;
}Here, Details is the detail view in the graph. If a detail record exists, insertion is disabled, so users cannot add another row through the grid.
2. Enforce the rule in RowInserting
Disabling the button only controls the UI. To enforce the rule at the graph level, validate the insertion as well.
protected virtual void _(Events.RowInserting<MyDetail> e)
{
if (e.Row == null)
return;
MyDetail existingRow = Details.SelectWindowed(0, 1);
if (existingRow != null)
{
e.Cancel = true;
throw new PXException(
"Only one detail row is allowed per document.");
}
}Replace MyHeader, MyDetail, and Details view with your actual DACs and view. If the rule depends on a specific business condition, apply that condition consistently in both events.
Hope, it helps!
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.