Setting a default value of null to the cell in the dev express grid if the user has not entered a value during inline editing

StackOverflow https://stackoverflow.com/questions/14309309

Question

This is a partial view called from the index. In the model the JobStatusSortOrder is set as a nullable double. Here if the user does not enter any value during inline editing, how can i set the value to null by default. I have tried looking for options like default text and I cant find any to help me. Could someone please help me with this? Thanks!

@model MyProject.Web.ViewModels.ProjectStatusListViewModel

@using MyProject.Data.Models;
@using System.Web.UI.WebControls;
@using DevExpress.Web.Mvc.UI;
@using DevExpress.Web.ASPxEditors;
@using DevExpress.Web.Mvc;
@using DevExpress.Web.ASPxGridView;

@{
Html.EnableClientValidation(true);

var grid = Html.DevExpress().GridView(
    settings =>
    {
        settings.Name = "gvProjectStatus";
        settings.KeyFieldName = "JobStatusID";
        settings.Width = Unit.Pixel(1080);
        settings.CallbackRouteValues = new { Controller = "Maintenance", Action = "ProjectStatusMasterPartial" };
        settings.ClientSideEvents.BeginCallback = "function(s,e){e.customArgs['id'] = '" + ViewContext.RouteData.Values["JobStatusID"] + "'}";

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Maintenance", Action = "JobStatusInlineAddNewPartial" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Maintenance", Action = "JobStatusInlineEditPartial" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Maintenance", Action = "JobStatusInlineDeletePartial" }; 

        settings.SettingsEditing.Mode = GridViewEditingMode.Inline;
        settings.SettingsBehavior.ConfirmDelete = true;

        //Command column
        settings.CommandColumn.Visible = true;
        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.Caption = "Actions";
        settings.CommandColumn.Width = 60; //Width is actually 100 not 60

        //for filtering
        settings.Settings.ShowFilterRow = true;
        settings.Settings.ShowFilterRowMenu = true;
        settings.CommandColumn.ClearFilterButton.Visible = true;

        settings.Columns.Add(column =>
        {
            column.FieldName = "JobStatusID";
            column.Caption = "JobStatusID";
            column.Visible = false;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "SiteID";
            column.Caption = "SiteID";
            column.Visible = false;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "JobStatusName";
            column.Caption = "Status Description";
            column.Settings.AllowAutoFilter = DefaultBoolean.True;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "JobStatusCurrent";
            column.Caption = "Is Current";
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.Settings.AllowAutoFilter = DefaultBoolean.False;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "JobStatusSortOrder";
            column.Caption = "Sort Order";
            column.Settings.AllowAutoFilter = DefaultBoolean.False;
        });
    });
}@grid.Bind(Model.JobStatus.ToList()).GetHtml()
Was it helpful?

Solution

Try this:

@{
Html.EnableClientValidation(true);

var grid = Html.DevExpress().GridView(
    settings =>
    {
        //your code
        //Add this code to initialize the rows for a New Row
        settings.InitNewRow = (sender, e) =>
        {
                e.NewValues["JobStatusSortOrder"] = 0; // set the default value as 0
        };
    });
}@grid.Bind(Model.JobStatus.ToList()).GetHtml()

OTHER TIPS

Since it is a numeric type, any empty value in the DevExpress grid should be equal to null in your data structure. Can't you just check the edited values for an empty string, and set them to null before sending to your data layer?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top