Is there any way to default the values that are used when I create a new row?
So for example, I have a row where I want to default a date to Jan 1st of the following year. However, when I add the row, it adds nulls to all fields and the date shows up as 1 Jan 1900 and there's a lot of fiddly clicking to set the right date.
Or, if the field is not nullable it would default to a zero or to a Jan 01 in the year 0001.
publicclassSampleData{publicSampleData(){// to set default values in the grid, use the default constructor of the model
HireDate = DateTime.Now.AddDays(14);
Salary =1000;}publicint ID {get;set;}publicstring Name {get;set;}publicDateTime HireDate {get;set;}publicdecimal Salary {get;set;}}
RAZOR
@* To set default values for the new row, use the default model constructor to set them
The Telerik grid and editors will show the values your app provides *@
Click the <strong>Add</strong> button to see the default values for the HireDate and Salary fields
<TelerikGrid Data=@MyData EditMode="@GridEditMode.Inline" Pageable="true" Height="500px"
OnUpdate="@UpdateHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler">
<GridToolBarTemplate><GridCommandButtonCommand="Add"Icon="@SvgIcon.Plus">Add Employee</GridCommandButton></GridToolBarTemplate><GridColumns>
<GridColumn Field=@nameof(SampleData.ID) Title="ID" Editable="false" />
<GridColumn Field=@nameof(SampleData.Name) Title="Name" />
<GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date (has default value)" />
<GridColumn Field=@nameof(SampleData.Salary) Title="Salary (has default value)" />
<GridCommandColumn><GridCommandButtonCommand="Save"Icon="@SvgIcon.Save"ShowInEdit="true">Save</GridCommandButton><GridCommandButtonCommand="Edit"Icon="@SvgIcon.Pencil">Edit</GridCommandButton><GridCommandButtonCommand="Delete"Icon="@SvgIcon.Trash">Delete</GridCommandButton><GridCommandButtonCommand="Cancel"Icon="@SvgIcon.Cancel"ShowInEdit="true">Cancel</GridCommandButton></GridCommandColumn></GridColumns></TelerikGrid>@code{asyncTaskUpdateHandler(GridCommandEventArgs args){SampleData item =(SampleData)args.Item;var index = MyData.FindIndex(i => i.ID == item.ID);if(index !=-1){
MyData[index]= item;}}asyncTaskDeleteHandler(GridCommandEventArgs args){SampleData item =(SampleData)args.Item;
MyData.Remove(item);}asyncTaskCreateHandler(GridCommandEventArgs args){SampleData item =(SampleData)args.Item;
item.ID = MyData.Count +1;
MyData.Insert(0, item);}publicList<SampleData> MyData {get;set;}protectedoverridevoidOnInitialized(){
MyData =newList<SampleData>();for(int i =0; i <50; i++){
MyData.Add(newSampleData(){
ID = i,
Name ="Name "+ i.ToString(),
HireDate = DateTime.Now.AddMonths(-i),
Salary = i^3});}}}
You can use the grid state to put the grid in insert/edit mode without the built-in "Add" command. You can add a button (your own or a GridCommandButton but with a custom command name) to the toolbar, and in its OnClick event you can set the InsertedItem of the grid state as desired.