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

I have problem with getting id row in grid AND refresh GRID without refresh browser

Hi 

I have problem with getting id row AND refresh GRID without refresh browser

Ofter saving(Insert Data to sql database) it needs get id( key "IsPrimaryKey and IsIdentity ") of  inserted row   or  refresh data in grid after inserting without browser refresh   


@{Html.EJ().Grid<Object>("FlatGrid") .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.data).UpdateURL("/TestTablesControllerView/CellEditUpdate").InsertURL("/TestTablesControllerView/CellEditInsert")
.RemoveURL("/TestTablesControllerView/CellEditDelete").Adaptor(AdaptorType.RemoteSaveAdaptor))

.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
 .AllowPaging()
  .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("Tkey").HeaderText("TKey").IsPrimaryKey(true).IsIdentity (true).Add();
col.Field("Tname").HeaderText("Tname").Add();
 col.Field("Tdesc").HeaderText("Tdesc").Add();
 col.Field("Tid").HeaderText("Tid"   ).Add();
 col.Field("Tate").HeaderText("TDate").Add();

   }).Render();

}





the set available code for get id after insert has technical problem :  "  value.Value.OrderID =order.LastOrDefault().OrderID + 1;"

I'm waiting for your prompt reply

thanks and regards.



1 Reply

VA Venkatesh Ayothi Raman Syncfusion Team April 7, 2017 12:51 PM UTC

Hi Hassan, 
Thanks for contacting Syncfusion support. 
Query #1: “Get the row ID while inserting the record” 
We went through your code example and found that you are using isIdentity property in Grid. If we enable the Isidentity property for Auto increment column then we must handle that Auto increment column.  
So, we are the responsibility to handle the Isidentity property. While performing CRUD operation in Grid with IsIdentity column, we need to return the modified value, because of we have handled IsPrimary key column value in server side. So, we suggest you to return the modified value while performing the CRUD operation in Grid like as follows, 
Code example
public ActionResult Insert([FromBody]OrdersView value) 
        { 
            value.OrderID = ++count; //Auto increment the value for isIdnetity property 
            order.Add(value); 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 

Another way
You may also set the isIdentity field value while saving the record in actionBegin event like as follows, 
Code example
@Grid 
 
@{Html.EJ().Grid<WebApplication8.Controllers.HomeController.Orders>("Editing") 
                            .Datasource(ds => ds.URL(@Url.Action("DataSource")).UpdateURL("/Home/CellEditUpdate") 
                      .InsertURL("/Home/CellEditInsert").RemoveURL("/Home/CellEditDelete").Adaptor(AdaptorType. RemoteSaveAdaptor)) 
                           .Columns(col => 
                           { 
                               col.Field("OrderID").IsPrimaryKey(true) .IsIdentity(true).TextAlign(TextAlign.Right).Width(90).Add(); 
                               . . . 
   
                           }).ClientSideEvents(e=>e.ActionBegin("actionBegin")) 
                           .Render(); 
 
              } 
@ActionBegin event 
 
<script type="text/javascript"> 
var flag=false, count=100;  
    function actionBegin(args) {         
        if (args.requestType == "add") 
            flag = true; 
 
        //Set the isIdentity value while inserting the record 
        if (flag && args.requestType == "save") { 
            args.data.OrderID = count++;// here OrderID is the isIdentity field and set the value while inserting the record 
            flag = false; 
        } 
    } 
</script> 

Query #2: “Refresh the Grid without refresh the browser” 
We can refresh the Grid by calling the refreshContent method in Grid. Here we can refresh the Grid using button click. Please refer to the below code example and Help document, 
Code example
<input type="button" value="Grid Refresh" id="refresh"/> 
 
@{Html.EJ().Grid<WebApplication8.Controllers.HomeController.Orders>("Editing") 
                            .Datasource(ds => ds.URL(@Url.Action("DataSource")).UpdateURL("/Home/CellEditUpdate")                      .InsertURL("/Home/CellEditInsert").RemoveURL("/Home/CellEditDelete").Adaptor(AdaptorType. RemoteSaveAdaptor)) 
                           .Columns(col => 
                           {                                
                               . . .   
                           }) 
                           .Render(); 
 
              } 
 
//Refresh the Grid using button click 
$("#refresh").ejButton({ 
        click: function () { 
            var gridObj = $("#Grid").ejGrid("instance"); 
            gridObj.refreshContent(); //Refresh the Grid by calling the refreshContent method 
        } 
    }); 
 

Regards, 
Venkatesh Ayothiraman. 


Loader.
Live Chat Icon For mobile
Up arrow icon