Datagrid Mode="EditMode.Dialog" how to add default values

When you click on add in the datagrid and the modal dialog pops up, how can we add default data to the fields.

I understand i can use a template but how do i go about creating one.. if i wanted to add two fields from a method

                                   
Special Release
                               
                               
                                   
                                            Toolbar="@(new List() { "Add", "Edit"})">
                                        @**@
                                       
                                       
                                       
                                       
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                       
                                   
                               

@code{
    [CascadingParameter]
    private Task authState { get; set; }
    private System.Security.Claims.ClaimsPrincipal principal;

    protected async override void OnParametersSet()
    {
        DateTime date = DateTime.UtcNow;
        if (authState != null)
        {
            //Getting  value from claim
            principal = (await authState).User;
            string defaultfield1 = principal.Claims.FirstOrDefault(c => c.Type == "sag_account_name").Value;
            DateTime defaultFieldTwo = date;
        }
    }

Plus add a model to the other fields.. thanks in advance.

So basically i would want the same fields as the normal text box editor but with two fields that you cannot edit at the bottom.

1 Reply 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team October 19, 2020 10:45 AM UTC

Hi Dingo, 

Greetings from Syncfusion. 

Query: Datagrid Mode="EditMode.Dialog" how to add default values 

We have validated your query and you want to add default values while adding the record. You can achieve your requirement by using DefaultValue property of GridColumn component. Find the below code snippets for your reference. 

 
<SfGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })" Height="315"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Dialog"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" ValidationRules="@(new ValidationRules{ Required=true})" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" DefaultValue="@("ANTON")" ValidationRules="@(new ValidationRules{ Required=true})" Width="120"></GridColumn> 
        . . . 
    </GridColumns> 
</SfGrid> 

Reference

Also, we might suspect that you want to disable editing for two fields. You can achieve this requirement by setting AllowEditing property as false for the corresponding fields. Find the below link for you reference. 


Please let us know if you have any concerns. 

Regards, 
Rahul 


Marked as answer
Loader.
Up arrow icon