How to assign (default) values of new record (row) in the code?

My class ParcelField used as TValue="ParcelField" has property annotated as "Required" in class definition:
[Required]
public string UpdateByLogin { get; set; }

This property is not visible on the grid to the user, because it should be automatically updated in the code. When user adds new record (row) on grid, then "Update" is not working (it blocks user from finishing editing of current row), because this property "UpdateByLogin" is still null. I want to set its value in the code. 

When and how is the proper place (event) to set property values of a new added record?

I tried to set value in: ActionBeginHandler and ActionCompletedHandler

if (args.Action=="Add")
{
args.RowData.UpdateByLogin = "Syncfusion";
}

, but it does not work. It still prevents user from finishing adding of new record because pressing Update does not finish editing of a new record. 




3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team September 24, 2020 08:40 AM UTC

Hi Waldemar, 

Greetings from Syncfusion support. 

We suggest you to use the OnActionComplete event of Grid. Based on the RequestType as Add, we suggest you to update the value for the corresponding column in args.Data of OnActionComplete event handler. Please refer and use as like the codes below, 

 
<GridEvents OnActionComplete="OnActionComplete" TValue="Order"></GridEvents> 

public void OnActionComplete(ActionEventArgs<Order> args){    if(args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add))    {        args.Data.CustomerID = "Syncfusion";    }}
[Required]public string CustomerID { getset; }

We are also attaching the sample for your reference, please download the sample from the link below, 
 
And also we would like to inform you that, we have DefaultValue property support in GridColumn. With this you can just define the default value as value for the DefaultValue of GridColumn(instead of using the above suggested OnActionComplete event solution). Please refer the below documentation link and code for more details, 

<GridColumn Field=@nameof(Order.CustomerID) HeaderText="CustomerID" Visible="false" DefaultValue="@DefaultVal" TextAlign="TextAlign.Right" Width="120"></GridColumn>public string DefaultVal = "Syncfusion";

Please use any of the above two suggestions to achieve this requirement. Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 


Marked as answer

WN Waldemar Nowak October 1, 2020 03:47 PM UTC

Thank you for the answer. It solves the problem. You may close this case.
Regards,
Waldemar Nowak


RS Renjith Singh Rajendran Syncfusion Team October 2, 2020 05:53 AM UTC

Hi Waldemar, 

Thanks for your update. 

We are glad to hear that the provided suggestion helped you in achieving your requirement. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 


Loader.
Up arrow icon