Articles in this section
Category / Section

how to change default value while adding

1 min read

While adding a new record in grid, by default in edit form it shows ‘0’ for number/integer column and “ ”(empty string) for string column. In certain cases, you may want to change that value in add edit form, this can be achieved by using defaultValue property of columns.

This property helps to define the specified default value while adding a new record in the Grid.

API Document link: https://help.syncfusion.com/api/js/ejgrid#members:columns-defaultvalue

JS

$("#Grid").ejGrid({                
                dataSource: window.gridData,
                allowPaging: true,               
                editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
                toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] },
                columns: [
                         { field: "OrderID", isPrimaryKey: true, headerText: "Order ID", defaultValue: 1000, textAlign: ej.TextAlign.Right, width: 90 },
                         { field: "CustomerID", headerText: 'Customer ID', defaultValue: "ABC", width: 90 },
                         { field: "EmployeeID", headerText: 'Employee ID', defaultValue: 5, editType: ej.Grid.EditingType.Dropdown, textAlign: ej.TextAlign.Right, width: 80},
                         { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, defaultValue: 1, editType: ej.Grid.EditingType.Numeric, , width: 80, format: "{0:C}" },
                ]
            });
 

 

MVC

@(Html.EJ().Grid<object>("FlatGrid")
        .Datasource(ds => ds.URL("OrderDataSource").Adaptor(AdaptorType.UrlAdaptor))
        .AllowPaging()
        .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
              .ToolbarSettings(toolbar =>
              {
                  toolbar.ShowToolbar().ToolbarItems(items =>
                  {
                      items.AddTool(ToolBarItems.Add);
                      items.AddTool(ToolBarItems.Edit);
                      items.AddTool(ToolBarItems.Delete);
                      items.AddTool(ToolBarItems.Update);
                      items.AddTool(ToolBarItems.Cancel);
                  });
              })
        .Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).DefaultValue(1000).TextAlign(TextAlign.Right).Width(75).Add();
            col.Field("CustomerID").HeaderText("Customer ID").DefaultValue("ABC").Width(80).Add();
            col.Field("EmployeeID").HeaderText("Employee ID").DefaultValue(5).EditType(EditingType.Dropdown).TextAlign(TextAlign.Right).Width(75).Add();
            col.Field("Freight").HeaderText("Freight").DefaultValue(1).TextAlign(TextAlign.Right).EditType(EditingType.Numeric).Width(75).Format("{0:C}").Add();
        }))

 

ASP

<ej:Grid  runat="server"  ID="Grid" AllowPaging="true" >
          
        <Columns>
 
                <ej:Column Field="OrderID" HeaderText="OrderID" IsPrimaryKey="true" DefaultValue=1000 TextAlign="Right" />
                <ej:Column Field="CustomerID"  HeaderText="CustomerID" DefaultValue="ABC" />
                <ej:Column Field="EmployeeID" EditType="Dropdown"  DefaultValue=5 Priority="2" HeaderText="EmployeeID "/>
                <ej:Column Field="Freight" HeaderText="Freight" EditType="Numeric" DefaultValue=1 Format="{0:C2}" TextAlign="Right" />
        
        </Columns>
       <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True" ></EditSettings>
                    <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings> 
    </ej:Grid>

 

The following screenshot shows the specified default values while adding the record.

Figure 1: Output

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (1)
Please  to leave a comment
JI
JIjo

actionBegin: function(args) { if (args.requestType == "add") { args.data.LocationId = localStorage.getItem('location'); }

}

actionComplete: function(args) { if (args.requestType == 'save') {

            localStorage.setItem('location', args.rowData.LocationId || 0);
        }
PK
Padmavathy Kamalanathan

Hi Jijo,

Greetings from Syncfusion Support.

From your update, we suspect that you are trying to set default value to one of your columns while adding new record to Grid. We can see that you are saving particular column value from rowData in actionComplete event with requestType “save” to the local storage. Then you retrieve the saved column value and set it to new record you are adding in the actionBegin event with requestType “add”.

We have created sample with your code, and it works at our end. Please check the below sample, https://jsplayground.syncfusion.com/qscvavbo

In the above sample, we have saved “EmployeeID” column value in local storage and used it to set default value to newly added records.

Still facing the issue, kindly share us the below details,

  1. Explain us your issue/requirement in detail with exact scenario
  2. Video demo of the issue
  3. Screenshot of error with complete stack trace (if any)
  4. If possible, share us issue reproducible sample or reproduce the issue in the sample shared above.

Regards, Padmavathy Kamalanathan

Access denied
Access denied