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