We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

how to set column cell value to 0 when null or empty

Hi,
I have a grid control with column named amount. and I need a functionality like this in that column cells. If the user do not enter a value or delete the existing value of that column cell it should always be 0. That means it should not be blank or null anytime. How do I do this. Please help.

Thank you
Kalum

3 Replies

MP Manivannan Padmanaban Syncfusion Team July 10, 2019 06:43 AM UTC

Hi kalum, 

Thanks for contacting Syncfusion Forums. 

Query: how to set column cell value to 0 when null or empty 

From the above query, we are able to understand that you want to set the value of amount column as ‘0’ when the value is entered in that column. Using default value property we can achieve your requirement. Kindly refer the below code example, 

@(Html.EJ().Grid<Object>("FlatGrid") 
………………………………………………………… 
                .Columns(col => 
                   { 
                       col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add(); 
                       col.Field("CustomerID").HeaderText("Customer ID").Add(); 
                       col.Field("Amount").HeaderText("Amount").DefaultValue(0).Add(); 
                       col.Field("Freight").HeaderText("Freight").EditType(EditingType.NumericEdit).DefaultValue(45).Add(); 
                       col.Field("ShipCountry").HeaderText("Ship Country").DefaultValue("Brazil").Add(); 
                   })) 

Refer the below help documentation link, 

If you need further assistance please get back to us, with the complete grid code example. 

Regards, 
Manivannan Padmanaban. 




KA kalum July 10, 2019 09:35 AM UTC

Hi Manivannan,
Thank you for the response. But in my case I'm using batch edit mode and looks it is not working when I delete everyting in the cell. Any help please.
Thanks
Kalum



MP Manivannan Padmanaban Syncfusion Team July 11, 2019 04:05 AM UTC

Hi Kalum, 

Thanks for the update. 

Query:  But in my case I'm using batch edit mode and looks it is not working when I delete everyting in the cell. 
 
In batch edit mode, by default the value is set while adding the new record based on type. For number column, the default value 0 is set and for string column the default value empty is set. Generally in grid the type is set based on the dataSource. If you have not bound the dataSource empty then, the type for the columns are not set by default. In this case, you have to manually set the type for the column. Refer the below code example, 


@(Html.EJ().Grid<Object>("FlatGrid") 
……………………………………………… 
                    .Columns(col => 
                       { 
                           col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Type("number").Add(); 
                           col.Field("CustomerID").HeaderText("Customer ID").EditType(EditingType.StringEdit).Type("string").Add(); 
                           col.Field("Freight").HeaderText("Freight").Type("number").Add(); 
                           col.Field("ShipCountry").HeaderText("Ship Country").Type("string").Add(); 
                           col.Field("OrderDate").HeaderText("Order Date").EditType(EditingType.Datepicker).Type("date").Format("{0:MM/dd/yyyy}").Add(); 
                       })) 




And also, we want to get the default value for amount column as 0 when the value is deleted. Using before BeforeBatchSave event, we can set the value for the column when is empty while saving. Refer the below code example, 

@(Html.EJ().Grid<Object>("FlatGrid") 
……………………………………………………………………… 
                       .ClientSideEvents(e=>e.BeforeBatchSave("BeforeBatchSave")) 
                    .Columns(col => 
                       { 
………………………………………………………. 
                       })) 
 
<script> 
    function BeforeBatchSave(args) { 
        if (ej.isNullOrUndefined(args.batchChanges.added[0].Freight)) { 
            args.batchChanges.added[0].Freight = 0;  // set the value here. 
        } 
    } 
</script> 

Still facing the issue, kindly get back to us with the below details. 

  1. Share complete grid code example.
  2. Share the video demonstration of this issue.

Regards, 
Manivannan Padmanaban. 


Loader.
Live Chat Icon For mobile
Up arrow icon